← Back to JavaScript, page 3

javascript-0059 · Easy fresher

Which array methods find items or test conditions (find, findIndex, some, every, includes, indexOf), and how do they differ?

find returns the first matching item and findIndex its position, some says whether any item matches, every whether all do, and includes or indexOf look for an exact value. includes handles NaN while indexOf does not, and some and every stop as soon as the answer is known. findLast and findLastIndex search from the end.

Full answer

These methods answer different questions about an array, and choosing the right one makes code shorter and clearer.

includes(value) returns true or false for an exact value, using SameValueZero comparison, so [NaN].includes(NaN) is true. indexOf(value) returns the position or -1 and uses strict equality, so it cannot find NaN. For objects, both compare by reference.

find(callback) returns the first item for which the callback is truthy, or undefined when none match. findIndex(callback) returns its position, or -1. Because find returns undefined for "not found", it cannot tell a missing item from a stored undefined, and findIndex can. findLast and findLastIndex do the same from the end of the array.

some(callback) returns true if at least one item passes and stops at the first one that does. every(callback) returns true only if all items pass and stops at the first failure. On an empty array, some returns false and every returns true, which is a common interview question.

filter returns all matches as a new array, so use find or some when only one match or a yes/no answer is needed, as they can stop early. at(-1) reads the last item. flat and flatMap are often used before searching nested lists.

Sparse arrays behave differently: find and findIndex visit every index and see empty slots as undefined, while some, every, and forEach skip empty slots.

javascript
const students = [
  { name: 'Asha', marks: 82 },
  { name: 'Ravi', marks: 35 },
  { name: 'Meera', marks: 91 },
  { name: 'Dev', marks: 35 },
];

console.log(students.find((s) => s.marks < 40));
console.log(students.findIndex((s) => s.marks < 40), students.findLastIndex((s) => s.marks < 40));
console.log(students.findLast((s) => s.marks < 40).name);
console.log(students.find((s) => s.marks > 100), students.findIndex((s) => s.marks > 100));

console.log(students.some((s) => s.marks > 90), students.every((s) => s.marks >= 35));
console.log([].some(() => true), [].every(() => false));

const numbers = [10, NaN, 30];
console.log(numbers.includes(NaN), numbers.indexOf(NaN), numbers.indexOf(30));
console.log(students.includes({ name: 'Asha', marks: 82 })); // different object

let checks = 0;
[5, 12, 8, 20].some((n) => {
  checks += 1;
  return n > 10;
});
console.log('callbacks run by some:', checks);

console.log(students.at(-1).name, [[1, 2], [3]].flat(), ['a b', 'c'].flatMap((x) => x.split(' ')));
console.log([1, undefined, 3].find((x) => x === undefined), [1, undefined, 3].findIndex((x) => x === undefined));

Run with Node.js v24.22.1 as js-59.js; two runs gave identical output. Output: { name: 'Ravi', marks: 35 } 1 3 Dev undefined -1 true true false true true -1 2 false callbacks run by some: 2 Dev [ 1, 2, 3 ] [ 'a', 'b', 'c' ] undefined 1

Key points

  • includes checks for a value and finds NaN; indexOf returns a position and does not.
  • find gives the first matching item; findIndex gives its index or -1.
  • some is "any", every is "all"; both stop early.
  • some on [] is false; every on [] is true.
  • findLast and findLastIndex search from the end.

Common mistakes

  • Using filter(...)[0] when find is enough.
  • Using indexOf to look for NaN.
  • Using includes with objects and expecting content comparison.
  • Forgetting that every on an empty array is true.

Follow-ups

  • How would you check whether two arrays share at least one item?
  • What does find return when the array contains undefined, and how do you handle it?
  • How is flatMap different from map followed by flat?

Tips

  • Start with the question each method answers: first item, position, any, or all.
  • Mention the empty-array results; they are a favorite trick question.
  • Say that find, some, and every stop early, which makes them cheaper than filter.