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…
find visits values until its predicate is true and returns that original value. It returns undefined when nothing matches. The arrow callback asks whether each number is greater than three. Our helper converts an absent match into explicit null using the ternary expression you learned in Lesson 30. This is different from filter, which always returns an array of all matches. findIndex returns a position rather than a value and uses -1 when there is no match.
function solve(numbers) {
const match = numbers.find(number => number > 3);
return match === undefined ? null : match;
}
console.log(solve([1,5,9]));5
find visits values until its predicate is true and returns that original value. It returns undefined when nothing matches. The arrow callback asks whether each number is greater than three. Our helper converts an absent match into explicit null using the ternary expression you learned in Lesson 30. This is different from filter, which always returns an array of all matches. findIndex returns a position rather than a value and uses -1 when there is no match.
function solve(numbers) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
const match = numbers.find(number => number > 3);Return the first value whose callback is true, or undefined if no value matches.
return match === undefined ? null : match;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([1,5,9]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(numbers) {
const match = numbers.find(number => number > 3);
return match === undefined ? null : match;
}
console.log(solve([0,2]));null
find visits values until its predicate is true and returns that original value. It returns undefined when nothing matches. The arrow callback asks whether each number is greater than three. Our helper converts an absent match into explicit null using the ternary expression you learned in Lesson 30. This is different from filter, which always returns an array of all matches. findIndex returns a position rather than a value and uses -1 when there is no match.
function solve(numbers) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
const match = numbers.find(number => number > 3);Return the first value whose callback is true, or undefined if no value matches.
return match === undefined ? null : match;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(numbers) {
const match = numbers.find(number => number > 3);
return match === undefined ? null : match;
}
console.log(solve([]));null
find visits values until its predicate is true and returns that original value. It returns undefined when nothing matches. The arrow callback asks whether each number is greater than three. Our helper converts an absent match into explicit null using the ternary expression you learned in Lesson 30. This is different from filter, which always returns an array of all matches. findIndex returns a position rather than a value and uses -1 when there is no match.
function solve(numbers) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
const match = numbers.find(number => number > 3);Return the first value whose callback is true, or undefined if no value matches.
return match === undefined ? null : match;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(numbers) {
const match = numbers.find(number => number < 3);
return match === undefined ? null : match;
}
console.log(solve([1,5,9]));function solve(numbers) {
const match = numbers.find(number => number > 3);
return match === undefined ? null : match;
}
console.log(solve([1,5,9]));Check for a missing match explicitly; find does not return an empty array.
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.