Ready. Start with one instruction.
Loading the learning workspace…
FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION
Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.
Predict → Experiment → Build → Test → Explain
Ready. Start with one instruction.
Loading the learning workspace…
An array is an ordered collection written with square brackets and comma-separated values. The literal in each function call creates the collection. Array length reports its number of positions, and index zero accesses the first element. This helper returns an explicit null for an empty array before trying an indexed read. The empty-array guard is a policy we wrote, not a general rule that all invalid indexes return null. Arrays are objects, but their ordered positions make them useful for lists.
function solve(items) {
if (items.length === 0) return null;
return items[0];
}
console.log(solve([0,2]));0
An array is an ordered collection written with square brackets and comma-separated values. The literal in each function call creates the collection. Array length reports its number of positions, and index zero accesses the first element. This helper returns an explicit null for an empty array before trying an indexed read. The empty-array guard is a policy we wrote, not a general rule that all invalid indexes return null. Arrays are objects, but their ordered positions make them useful for lists.
function solve(items) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
if (items.length === 0) return null;Check the stated condition. This guard returns an explicit missing-value result before attempting the indexed read.
return items[0];Evaluate this expression, end the function and give the resulting value to its caller. It is not automatically printed.
}Close the current instruction block.
console.log(solve([0,2]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(items) {
if (items.length === 0) return null;
return items[0];
}
console.log(solve(["Code"]));Code
An array is an ordered collection written with square brackets and comma-separated values. The literal in each function call creates the collection. Array length reports its number of positions, and index zero accesses the first element. This helper returns an explicit null for an empty array before trying an indexed read. The empty-array guard is a policy we wrote, not a general rule that all invalid indexes return null. Arrays are objects, but their ordered positions make them useful for lists.
function solve(items) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
if (items.length === 0) return null;Check the stated condition. This guard returns an explicit missing-value result before attempting the indexed read.
return items[0];Evaluate this expression, end the function and give the resulting value to its caller. It is not automatically printed.
}Close the current instruction block.
console.log(solve(["Code"]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(items) {
if (items.length === 0) return null;
return items[0];
}
console.log(solve([]));null
An array is an ordered collection written with square brackets and comma-separated values. The literal in each function call creates the collection. Array length reports its number of positions, and index zero accesses the first element. This helper returns an explicit null for an empty array before trying an indexed read. The empty-array guard is a policy we wrote, not a general rule that all invalid indexes return null. Arrays are objects, but their ordered positions make them useful for lists.
function solve(items) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
if (items.length === 0) return null;Check the stated condition. This guard returns an explicit missing-value result before attempting the indexed read.
return items[0];Evaluate this expression, end the function and give the resulting value to its caller. It is not automatically printed.
}Close the current instruction block.
console.log(solve([]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
Repair the intentionally changed operation so the example produces the exact expected result. Then try the extension without copying the answer.
function solve(items) {
if (items.length === 0) return null;
return items[1];
}
console.log(solve([0,2]));function solve(items) {
if (items.length === 0) return null;
return items[0];
}
console.log(solve([0,2]));The first element is at index zero; the length is not a valid final index.
Which operation changed the result, why did it work, and what would you test next?
Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.
Related: Previous lesson Next lesson
100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.