Array Find and Search Methods encompass functions such as indexOf(), lastIndexOf(), and includes() for locating elements within arrays.
Additionally, see the related sections on Basic Methods, Sort Methods, and Iteration Methods.
The indexOf() method scans an array for a specified element value and returns its position.
Note: The first item occupies position 0, the second item occupies position 1, and so forth. |
Example
Look for the item “Apple” within the array.
const fruits = [“Apple”, “Orange”, “Apple”, “Mango”]; let position = fruits.indexOf(“Apple”) + 1; |
array.indexOf(item, start) |
item |
Mandatory. The item to be searched for. |
start |
Optional. Indicates the starting point for the search. Negative values will initiate the search from the specified position counting from the end, and continue until the end of the array. |
Array.indexOf() returns -1 if the item is not found within the array.
If the item is present multiple times, it returns the position of the first occurrence.
Array.lastIndexOf() functions similarly to Array.indexOf(), but it returns the position of the last occurrence of the specified element.
Example
Look for the item “Apple” within the array.
const fruits = [“Apple”, “Orange”, “Apple”, “Mango”]; let position = fruits.lastIndexOf(“Apple”) + 1; |
array.lastIndexOf(item, start) |
item |
Mandatory. The item to be searched for. |
start |
Optional. Specifies the starting point for the search. Negative values will initiate the search from the specified position counting from the end, and continue towards the beginning of the array. |
In ECMAScript 2016, Array.includes() was introduced to arrays. This method enables us to determine if an element is included in an array, including the ability to check for NaN, unlike indexOf().
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.includes(“Mango”); // is true |
array.includes(search-item) |
Array.includes() permits checking for NaN values, which differs from Array.indexOf(). |
The includes() method is a feature introduced in ECMAScript 2016.
ES2016 is fully supported in all modern browsers as of March 2017.
includes() is not compatible with Internet Explorer.
The find() method retrieves the value of the initial array element that satisfies a specified test function.
In this example, it identifies (and returns the value of) the first element greater than 18:
Example
const numbers = [4, 9, 16, 25, 29]; let first = numbers.find(myFunction); function myFunction(value, index, array) { return value > 18; } |
Note that the function accepts three arguments:
The find() method is an ES6 feature (JavaScript 2015).
ES6 has been fully supported in all modern browsers since June 2017.
find() is not compatible with Internet Explorer.
The findIndex() method retrieves the index of the initial array element that satisfies a specified test function.
In this example, it identifies the index of the first element greater than 18:
Example
const numbers = [4, 9, 16, 25, 29]; let first = numbers.findIndex(myFunction); function myFunction(value, index, array) { return value > 18; } |
Please note that the function accepts three arguments:
The findIndex() method is an ES6 feature (JavaScript 2015).
ES6 has been fully supported in all modern browsers since June 2017.
findIndex() is not compatible with Internet Explorer.
In ES2023, the findLast() method was introduced. It begins searching from the end of an array and returns the value of the first element that meets a specified condition.
Example
const temp = [27, 28, 30, 40, 42, 35, 30]; let high = temp.findLast(x => x > 40); |
findLast() is a feature introduced in ES2023.
It has been supported in all modern browsers since July 2023.
The findLastIndex() method locates the index of the last element that meets a specified condition.
Example
const temp = [27, 28, 30, 40, 42, 35, 30]; let pos = temp.findLastIndex(x => x > 40); |
findLastIndex() is a feature introduced in ES2023.
It has been supported in all modern browsers since July 2023.