Syntax
arr.indexOf(value)
arr.includes(value)
arr.lastIndexOf(value)Examples
indexOf() and includes()
Finding whether and where a value exists in an array.
const fruits = ["apple", "banana", "cherry", "banana"];
console.log(fruits.indexOf("banana")); // 1 - first match
console.log(fruits.indexOf("grape")); // -1 - not found
console.log(fruits.includes("cherry")); // true
console.log(fruits.includes("grape")); // falselastIndexOf()
Searching for the last occurrence of a value.
const fruits = ["apple", "banana", "cherry", "banana"];
console.log(fruits.lastIndexOf("banana")); // 3 - the last matchWhy includes() Cannot Find Objects by Content
A common gotcha - these methods compare by reference, not deep equality.
const users = [{ name: "Alice" }, { name: "Bob" }];
console.log(users.includes({ name: "Alice" })); // false - different object reference!
// Use .some() with a custom comparison instead
console.log(users.some(u => u.name === "Alice")); // trueBest practices
- Use includes() when you only need a true/false answer - it is more readable than checking indexOf() !== -1
- Use indexOf() when you actually need the position of the match, such as for use with splice()
- Remember these methods use strict equality, so they cannot find objects/arrays by their content - use find() or some() with a custom comparison for that
- includes() correctly finds NaN (unlike indexOf(), which cannot) - a subtle but useful difference
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Array Basics
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().