Searching Array Elements
Learn how to search arrays using indexOf(), lastIndexOf(), includes(), find(), findIndex(), some(), and every() — including return values, performance, and when to use each one.
Reading Time
20 min
Lesson
Lesson 18 of 48
Introduction
Once you know how to add and remove elements from an array, the next common task is finding something inside it. JavaScript gives you two families of searching methods. The first family — indexOf(), lastIndexOf(), and includes() — looks for an exact value match using strict equality (===), which works well for primitives like numbers and strings. The second family — find(), findIndex(), some(), and every() — takes a callback function, which lets you search based on a condition instead of an exact value, making it the right choice for arrays of objects or more complex matching logic. None of these methods mutate the original array; they only read it and return a result.
Methods Overview
| Method | Purpose | Returns | Stops at First Match? |
|---|---|---|---|
| indexOf() | Find the index of an exact value | Index number, or -1 | ✅ Yes |
| lastIndexOf() | Find the index of the last exact match | Index number, or -1 | ✅ Yes (searches backward) |
| includes() | Check if a value exists | true or false | ✅ Yes |
| find() | Get the first element matching a condition | The matching element, or undefined | ✅ Yes |
| findIndex() | Get the index of the first match | Index number, or -1 | ✅ Yes |
| some() | Check if at least one element matches | true or false | ✅ Yes |
| every() | Check if all elements match | true or false | ❌ No (checks all, unless one fails) |
indexOf()
The indexOf() method searches the array for the first occurrence of a specific value and returns its index. If the value isn't found, it returns -1 instead of throwing an error, so it's common to check the result with an if statement or compare it directly against -1. indexOf() uses strict equality, which means it compares both value and type — so 1 and "1" are treated as different values. You can also pass a second argument, fromIndex, to tell indexOf() where in the array to start searching.
indexOf() Example
Console Output
Click “Run” to see the console output here.
lastIndexOf()
The lastIndexOf() method works just like indexOf(), except it searches the array from the end toward the beginning and returns the index of the last matching value. This is useful when duplicates exist and you specifically need the final occurrence rather than the first. Like indexOf(), it returns -1 if no match is found, and it also accepts an optional fromIndex argument to limit how far back it starts searching.
lastIndexOf() Example
Console Output
Click “Run” to see the console output here.
includes()
The includes() method is the simplest way to check whether an array contains a particular value — it returns a plain true or false instead of an index, which makes your code more readable when you don't actually need the position. Unlike indexOf(), includes() can correctly detect NaN in an array (indexOf() cannot, because NaN !== NaN under strict equality, but includes() uses a slightly different algorithm called SameValueZero).
includes() Example
Console Output
Click “Run” to see the console output here.
find()
The find() method takes a callback function and returns the first element for which that callback returns a truthy value. This makes it perfect for searching arrays of objects, where you can't just check for an exact value match. As soon as find() locates a matching element, it stops iterating and returns it — if no element matches, it returns undefined rather than -1.
find() Example
Console Output
Click “Run” to see the console output here.
findIndex()
The findIndex() method behaves exactly like find(), except it returns the index of the first matching element instead of the element itself. If no element satisfies the callback, it returns -1, matching the convention set by indexOf(). This is handy when you need the position so you can later use it with something like splice() to remove or replace that element.
findIndex() Example
Console Output
Click “Run” to see the console output here.
some()
The some() method checks whether at least one element in the array satisfies the given condition, returning true as soon as it finds one match, or false if it checks every element without finding one. It's the array equivalent of asking "does ANY of these qualify?" — useful for validation checks, like confirming a form field contains at least one uppercase letter.
some() Example
Console Output
Click “Run” to see the console output here.
every()
The every() method checks whether ALL elements in the array satisfy the given condition. It returns true only if every single element passes the test, and stops immediately and returns false as soon as it finds one element that fails. An important edge case: calling every() on an empty array always returns true — this is called "vacuous truth", since there are no elements to fail the check.
every() Example
Console Output
Click “Run” to see the console output here.
Which Method Should You Use?
Use indexOf(), lastIndexOf(), and includes() when you're searching for an exact primitive value like a number or string. Use find() and findIndex() when you're searching an array of objects or need a condition rather than an exact match — find() when you want the element itself, findIndex() when you want its position. Use some() when you only need a yes/no answer to "does at least one element match?", and every() when you need to confirm "do all elements match?".
Important
All of these methods are read-only — none of them modify the original array. Be careful with indexOf() and lastIndexOf() on arrays of objects: they use strict equality, so searching for { id: 1 } will not find an object that looks identical but is a different reference in memory. For object comparisons, use find() or findIndex() with a callback that checks a specific property instead.