HuntExams Academy logo
JavaScriptBeginner#arrays

What do find, findIndex, some and every do?

Answer

find returns the first matching element, findIndex its index, some returns true if any element matches, every returns true only if all match. some and every short-circuit.

Example

const u = [{age:17},{age:22}];
u.find(x => x.age > 18);   // {age:22}
u.some(x => x.age > 18);   // true
u.every(x => x.age > 18);  // false

Detailed explanation

JavaScript arrays provide find, findIndex, some, and every to search elements or test conditions without writing a manual loop. Each accepts a predicate: a callback whose return value is interpreted as truthy or falsy. The callback receives the current element, its index, and the array, although beginners usually need only the element. Choose the method based on the result you need: an element, an index, or a boolean. These methods do not directly modify the array, but their callbacks can cause mutations.

find returns the first element that satisfies the predicate, or undefined if nothing matches. findIndex returns the index of the first matching element, or -1 if nothing matches. For const u = [{age:17}, {age:22}], u.find(x => x.age > 18) returns the second object, while u.findIndex(x => x.age > 18) returns 1. The object returned by find is the original object reference, not a copy, so changing its properties also changes the object stored in the array. Both methods stop immediately after the first match.

some answers whether at least one element satisfies the predicate. For the same array, u.some(x => x.age > 18) returns true. every answers whether all elements satisfy it, so u.every(x => x.age > 18) returns false. Both short-circuit: some stops at the first truthy result, while every stops at the first falsy result. On an empty array, some returns false and every returns true: there is no matching element for some, and no element that violates the condition for every.

Under the hood, these methods examine indices in ascending order, using the array length captured when the call starts. They take O(n) time in the worst case, assuming constant-time predicates, but can finish earlier through short-circuiting. One subtle difference is that some and every skip empty slots in sparse arrays, whereas find and findIndex visit them as undefined. Interviewers usually look for correct return values, no-match behaviour, and appropriate method selection rather than memorised definitions.

Key points to remember

  • find returns the first matching element; no match produces undefined.
  • findIndex returns the first matching index; no match produces -1.
  • some returns whether at least one element matches; every returns whether all visited elements match.
  • All four methods short-circuit when their result is decided.
  • For empty arrays: find returns undefined, findIndex returns -1, some returns false, and every returns true.

Common mistakes candidates make

  • Checking findIndex with a truthiness test: index 0 is falsy and -1 is truthy; compare the result with -1.
  • Forgetting to return a condition from a callback with braces, such as x => { return x.age > 18; }.
  • Assuming find returns every match or a copied object; it returns only the first match and preserves object identity.

Likely follow-up questions

How is find different from filter?

find returns the first matching element and stops searching immediately. filter checks all present elements and returns a new array containing all matches, or an empty array if none match.

Why does every return true for an empty array?

every returns false only when it encounters an element that fails the condition. An empty array has no such element, so it returns true; require arr.length > 0 as well if emptiness should fail validation.

Can find distinguish a missing match from a matching undefined value?

No: both cases produce undefined. Use findIndex when you need to distinguish them, because a matching undefined element has an index, whereas no match returns -1.

Interview tip: Explain the four return types using one array, then mention no-match results and that all four methods short-circuit.

Related Interview Questions

1
JavaScriptBeginner#es6

What is destructuring?

Open
2
JavaScriptBeginner#es6

Difference between spread and rest operators?

Open
3
JavaScriptIntermediate#objects

How do you deep clone an object?

Open