Syntax
arr.find(item => condition)
arr.findIndex(item => condition)Examples
find() - Locating an Object by Property
The most common use case: finding a specific object in an array.
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
{ id: 3, name: "Charlie" }
];
const user = users.find(u => u.id === 2);
console.log(user); // { id: 2, name: "Bob" }
const notFound = users.find(u => u.id === 99);
console.log(notFound); // undefinedfindIndex() - Locating a Position
Getting the index of a match, useful before splice() or update operations.
const numbers = [4, 9, 16, 25, 36];
const index = numbers.findIndex(n => n > 15);
console.log(index); // 2 (the index of 16)
const notFoundIndex = numbers.findIndex(n => n > 100);
console.log(notFoundIndex); // -1findLast() and findLastIndex()
Searching from the end of the array instead of the beginning.
const numbers = [1, 5, 8, 5, 2];
console.log(numbers.findLast(n => n === 5)); // 5 (the last matching value)
console.log(numbers.findLastIndex(n => n === 5)); // 3 (its index)Best practices
- Use find() instead of filter()[0] when you only need the first match - it stops searching as soon as it finds one, which is more efficient
- Use findIndex() when you need the position for a subsequent operation like splice() or array update
- Prefer find()/findIndex() over indexOf() when searching by object property rather than exact primitive value
- Remember find() returns undefined (not -1) when nothing matches - check with a truthy check or === undefined, not === -1
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().