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…
Indexes begin at zero, so the second element is at index one. Before accessing that position, the guard requires at least two elements. The same code then works for a normal list, a one-element list and an empty list. Notice the distinction between array length and an index: length counts the positions, while the first position has index zero. Reading outside the array without a guard would return undefined.
function solve(items) {
if (items.length < 2) return null;
return items[1];
}
console.log(solve(["Learn","Build"]));Build
Indexes begin at zero, so the second element is at index one. Before accessing that position, the guard requires at least two elements. The same code then works for a normal list, a one-element list and an empty list. Notice the distinction between array length and an index: length counts the positions, while the first position has index zero. Reading outside the array without a guard would return undefined.
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 < 2) return null;Check the stated condition. This guard returns an explicit missing-value result before attempting the indexed read.
return items[1];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(["Learn","Build"]));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 < 2) return null;
return items[1];
}
console.log(solve(["Run"]));null
Indexes begin at zero, so the second element is at index one. Before accessing that position, the guard requires at least two elements. The same code then works for a normal list, a one-element list and an empty list. Notice the distinction between array length and an index: length counts the positions, while the first position has index zero. Reading outside the array without a guard would return undefined.
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 < 2) return null;Check the stated condition. This guard returns an explicit missing-value result before attempting the indexed read.
return items[1];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(["Run"]));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 < 2) return null;
return items[1];
}
console.log(solve([]));null
Indexes begin at zero, so the second element is at index one. Before accessing that position, the guard requires at least two elements. The same code then works for a normal list, a one-element list and an empty list. Notice the distinction between array length and an index: length counts the positions, while the first position has index zero. Reading outside the array without a guard would return undefined.
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 < 2) return null;Check the stated condition. This guard returns an explicit missing-value result before attempting the indexed read.
return items[1];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 < 2) return null;
return items[0];
}
console.log(solve(["Learn","Build"]));function solve(items) {
if (items.length < 2) return null;
return items[1];
}
console.log(solve(["Learn","Build"]));Use the array’s length to check that the desired position exists before reading it.
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.