🟢 EasyArrays📖 Theory Question

How do Array.find(), findIndex(), some(), and every() work?

💡

Hint

find=first match value, findIndex=first match index, some=any passes, every=all pass

Full Answer

  • find() — returns the first element where callback returns true, or undefined
  • findIndex() — returns the index of first match, or -1
  • some() — returns true if at least one element passes (short-circuits)
  • every() — returns true only if ALL elements pass (short-circuits)
const users = [
  { id: 1, name: 'Alice', active: true  },
  { id: 2, name: 'Bob',   active: false },
  { id: 3, name: 'Carol', active: true  },
];

users.find(u => u.id === 2);       // { id: 2, name: 'Bob', active: false }
users.findIndex(u => u.id === 2);  // 1
users.find(u => u.id === 99);      // undefined (not found)

users.some(u => u.active);  // true  (stops at Alice)
users.every(u => u.active); // false (stops at Bob)

// Short-circuit saves work
users.some(u => {
  console.log('checking', u.name);
  return u.name === 'Alice'; // only checks Alice — stops immediately
});
💡 some() is like logical OR across the array; every() is like AND. Use find() when you need the value, findIndex() when you need the position.

More Arrays Questions

🟢 EasyWhen would you use map vs forEach vs reduce vs filter?→🟢 EasyHow do Array.flat() and Array.flatMap() work?→🟢 EasyWhat are immutable array operations and the new ES2023 methods?→

Practice this in a timed sprint →

5 free questions, no signup required

âš¡ Start Sprint