Sub topic 11
Finding Elements and Testing Conditions
This chapter assumes you know arrow functions, objects, and the (element, index, array) callback signature from the earlier chapter on callbacks. The methods below answer a question about an array without changing it. None of them mutate the array.
Two kinds of search
Some methods take a value and compare it to each element. Others take a predicate, which is a callback that returns true or false for each element.
| Method | Takes | Returns when found | Returns when not found |
|---|---|---|---|
| `indexOf(value)` | value | first index | `-1` |
| `lastIndexOf(value)` | value | last index | `-1` |
| `includes(value)` | value | `true` | `false` |
| `find(fn)` | predicate | first matching element | `undefined` |
| `findIndex(fn)` | predicate | first matching index | `-1` |
| `findLast(fn)` | predicate | last matching element | `undefined` |
| `findLastIndex(fn)` | predicate | last matching index | `-1` |
| `some(fn)` | predicate | `true` if any match | `false` |
| `every(fn)` | predicate | `true` if all match | `false` |
Value-based methods compare with strict equality, so they work well for primitives. Two different objects are never equal, even when their contents match, so use a predicate to search arrays of objects.
const users = [
{ name: 'Ana', age: 31 },
{ name: 'Ben', age: 17 },
{ name: 'Cleo', age: 24 }
];
console.log(users.find(u => u.age < 18)); // { name: 'Ben', age: 17 }
console.log(users.findIndex(u => u.name === 'Cleo')); // 2
console.log(users.findLast(u => u.age > 20)); // { name: 'Cleo', age: 24 }
console.log(users.some(u => u.age < 18)); // true
console.log(users.every(u => u.age > 10)); // true
console.log([1, 2, 3].includes(2)); // true
console.log(users.includes({ name: 'Ana', age: 31 })); // false (different object)Choosing the right method
Common errors
Treating -1 or undefined as falsy
-1 is truthy, so if (arr.indexOf(x)) is wrong. It fails when the match is at index 0 and passes when there is no match at all. Use includes for a yes/no check, or compare against -1 explicitly.
const tags = ['js', 'css'];
if (tags.indexOf('js')) console.log('never runs: index is 0');
if (tags.includes('js')) console.log('found js');
if (tags.indexOf('html') !== -1) console.log('never runs');find can also be ambiguous. When the matching element is itself undefined, 0, or '', the result looks the same as a miss in a truthiness check. If the element could be falsy, use findIndex and test the index.
NaN
indexOf uses strict equality, and NaN !== NaN, so indexOf never finds NaN. includes uses SameValueZero comparison, which treats NaN as equal to itself.
console.log([NaN].indexOf(NaN)); // -1
console.log([NaN].includes(NaN)); // trueEmpty arrays
On an empty array, some returns false and every returns true. This is vacuous truth: no element breaks the rule. Remember it when you write checks like "all items are valid" before any items exist.
Exercise
Given const scores = [72, 0, 88, 95], write one expression that checks whether every score is a passing mark of at least 50, and another that returns the position of the first score above 90.
Show answer
scores.every(s => s >= 50) returns false because of the 0. scores.findIndex(s => s > 90) returns 3.
Exercise
Why does if (items.find(x => x === 0)) fail to detect a 0 in the array, and how do you fix it?
Show answer
find returns the matching element, 0, which is falsy, so the if block is skipped. Use items.includes(0) or items.findIndex(x => x === 0) !== -1.