JavaScript Array Search
Finding things in an array is one of the most common tasks in programming: is this user in the list, where is that product, which order is overdue? JavaScript gives you two families of search methods — ones that look for a value, and ones that test each element with a function.
Searching for a value
| Method | Returns |
|---|---|
includes(value) |
true / false |
indexOf(value) |
first index, or -1 |
lastIndexOf(value) |
last index, or -1 |
const tags = ["js", "css", "html", "js", "node"];
console.log(tags.includes("css"));
console.log(tags.includes("python"));
console.log(tags.indexOf("js"));
console.log(tags.lastIndexOf("js"));
console.log(tags.indexOf("python"));
console.log(tags.indexOf("js", 1)); // start searching at index 1true
false
0
3
-1
3
These methods compare with strict equality (===), with one exception: includes can find NaN, while indexOf can't.
const values = [1, NaN, "2"];
console.log(values.includes(NaN), values.indexOf(NaN));
console.log(values.includes(2)); // "2" is a stringtrue -1
false
Searching with a test function
When you're looking for something that matches a condition — "the first product under 10 €", "the user with id 7" — pass a function. It's called for each element and should return true for a match.
| Method | Returns |
|---|---|
find(fn) |
first matching element, or undefined |
findIndex(fn) |
index of the first match, or -1 |
findLast(fn) |
last matching element, or undefined |
findLastIndex(fn) |
index of the last match, or -1 |
filter(fn) |
new array of all matches (possibly empty) |
some(fn) |
true if at least one element matches |
every(fn) |
true if all elements match |
const products = [
{ id: 1, name: "Mug", price: 8, stock: 0 },
{ id: 2, name: "Poster", price: 15, stock: 4 },
{ id: 3, name: "Pen", price: 2, stock: 30 },
{ id: 4, name: "Notebook", price: 6, stock: 12 },
];
console.log(products.find((p) => p.id === 3));
console.log(products.findIndex((p) => p.name === "Poster"));
console.log(products.findLast((p) => p.price < 10).name);
console.log(products.find((p) => p.price > 100));{ id: 3, name: 'Pen', price: 2, stock: 30 }
1
Notebook
undefined
filter, some and every
filter collects every match into a new array. some and every answer yes/no questions about the whole array, and stop as soon as the answer is known:
const products = [
{ name: "Mug", price: 8, stock: 0 },
{ name: "Poster", price: 15, stock: 4 },
{ name: "Pen", price: 2, stock: 30 },
];
const cheap = products.filter((p) => p.price < 10);
console.log(cheap.map((p) => p.name));
console.log(products.some((p) => p.stock === 0)); // anything sold out?
console.log(products.every((p) => p.price > 0)); // all have a price?
console.log([].every((p) => false)); // vacuously true[ 'Mug', 'Pen' ]
true
true
true
Searching objects by reference
includes and indexOf compare objects by reference, so a look-alike object won't be found. Search by a property with find/some instead:
const users = [{ name: "Ola" }, { name: "Per" }];
console.log(users.includes({ name: "Ola" }));
console.log(users.some((u) => u.name === "Ola"));false
true
Tip: If you look up items by a key over and over (for example by
id), convert the array to aMaponce.map.get(id)is much faster than scanning a long array withfindevery time.
Which method?
| Question | Use |
|---|---|
| Is this exact value in the array? | includes |
| Where is this exact value? | indexOf |
| Give me the first element that… | find |
| Give me all elements that… | filter |
| Is there at least one that…? | some |
| Do all elements…? | every |
Test yourself
What does find return when nothing matches?
- -1
- null
- undefined
- An empty array
What is [3, 8, 12].some(n => n > 10)?
- true
- false
- 12
- [12]
Which method returns ALL matching elements?
- find
- filter
- findIndex
- some