TExcelTAD
Home › JavaScript › JS Array Search

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
js · try it yourself
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 1
true
false
0
3
-1
3

These methods compare with strict equality (===), with one exception: includes can find NaN, while indexOf can't.

js · try it yourself
const values = [1, NaN, "2"];
console.log(values.includes(NaN), values.indexOf(NaN));
console.log(values.includes(2));    // "2" is a string
true -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
js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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 a Map once. map.get(id) is much faster than scanning a long array with find every 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. -1
  2. null
  3. undefined
  4. An empty array

What is [3, 8, 12].some(n => n > 10)?

  1. true
  2. false
  3. 12
  4. [12]

Which method returns ALL matching elements?

  1. find
  2. filter
  3. findIndex
  4. some