J

JavaScript Handbook

Clean • Professional

JavaScript Array Sorting Methods

2 minute

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.

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

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

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

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);

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);

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);

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

MethodReturnsDirectionComparison Type
indexOfFirst index or -1Left → RightStrict (===)
lastIndexOfLast index or -1Right → LeftStrict (===)
includesBooleanLeft → RightStrict (===)
findElement or undefinedLeft → RightCustom (callback)
findIndexIndex or -1Left → RightCustom (callback)
findLastElement or undefinedRight → LeftCustom (callback)
findLastIndexIndex or -1Right → LeftCustom (callback)


Article 0 of 0