QUESTION 19 / 20 · ARRAYS · BEGINNER

Stop at the first match

What will this code print? Read each line, then make your prediction.

Where this matters

Use find for one matching item, such as the first available appointment. Use filter when you need every match.

JavaScript · read-only example
console.log([2, 7, 9].find(n => n > 5));
Your prediction

Read the full explanation

Find returns the first matching element. Seven matches before nine, so iteration stops there.

Trace the code

  1. The predicate is false for 2, so the search continues.
  2. It is true for 7, so find returns that element immediately.
  3. The later matching value 9 is not needed. The return value is not wrapped in an array.

Why the other predictions differ

  • [7,9] — That would be the result of filter, which collects every match.
  • 1 — That is the index of the match; find returns the value, not its index.
Expected console output
7

Reading this explanation does not record mastery. A correct submitted attempt can do so, including after review.

Change one thing

Choose a condition no entry satisfies.

Predict the result, then run this self-contained snippet in your browser’s developer console. It uses only sample values and makes no network requests.

console.log([2, 7, 9].find(n => n > 10));
Check the variation’s output
undefined

Explain which binding, value, or operation changed before moving to the next challenge.