JavaScript Array Search Methods
These methods are used to locate elements or values in an array. Some return the element itself, some return the index, and some return a boolean.
Array indexOf()
Returns the first index of a specified element, or -1 if not found. Uses strict equality (===
).
const fruits = ["Apple", "Banana", "Orange", "Banana"];
console.log(fruits.indexOf("Banana")); // 1
console.log(fruits.indexOf("Banana", 2)); // 3
console.log(fruits.indexOf("Grape")); // -1
Array lastIndexOf()
Returns the last index of a specified element, or -1 if not found. Searches from right to left.
const fruits = ["Apple", "Banana", "Orange", "Banana"];
console.log(fruits.lastIndexOf("Banana")); // 3
console.log(fruits.lastIndexOf("Banana", 2)); // 1
console.log(fruits.lastIndexOf("Grape")); // -1
Array includes()
Checks if an array contains a specified element, returning true or false.
const fruits = ["Apple", "Banana", "Orange"];
console.log(fruits.includes("Banana")); // true
console.log(fruits.includes("Banana", 2)); // false
console.log(fruits.includes("Grape")); // false
Array find()
Returns the first element that satisfies a testing function, or undefined if none match.
const numbers = [5, 12, 8, 130, 44];
const found = numbers.find(num => num > 10); // 12
console.log(found);
const users = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }];
const user = users.find(u => u.id === 2); // { id: 2, name: "Bob" }
console.log(user);
Array findIndex()
Returns the index of the first element that satisfies a testing function, or -1 if none match.
const numbers = [5, 12, 8, 130, 44];
const index = numbers.findIndex(num => num > 10); // 1
console.log(index);
const users = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }];
const userIndex = users.findIndex(u => u.name === "Bob"); // 1
console.log(userIndex);
Array findLast()
Returns the last element that satisfies a testing function, searching from the end.
const numbers = [5, 12, 8, 130, 44];
const last = numbers.findLast(num => num > 10); // 44
console.log(last);
const users = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Bob" }];
const lastUser = users.findLast(u => u.name === "Bob"); // { id: 3, name: "Bob" }
console.log(lastUser);
Array findLastIndex()
Returns the index of the last element that satisfies a testing function, searching from the end.
const numbers = [5, 12, 8, 130, 44];
const lastIndex = numbers.findLastIndex(num => num > 10); // 4
console.log(lastIndex);
const users = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Bob" }];
const lastUserIndex = users.findLastIndex(u => u.name === "Bob"); // 2
console.log(lastUserIndex);
Comparison Table
Method | Returns | Direction | Comparison Type |
---|---|---|---|
indexOf | First index or -1 | Left → Right | Strict (===) |
lastIndexOf | Last index or -1 | Right → Left | Strict (===) |
includes | Boolean | Left → Right | Strict (===) |
find | Element or undefined | Left → Right | Custom (callback) |
findIndex | Index or -1 | Left → Right | Custom (callback) |
findLast | Element or undefined | Right → Left | Custom (callback) |
findLastIndex | Index or -1 | Right → Left | Custom (callback) |