Array Search in JavaScript

Array search and find methods

See also:

JavaScript Array indexOf()

indexOf() The method searches for an element value in the array and returns its position.

Note:The position of the first item is 0, the position of the second item is 1, and so on.

Example

Search for the item "Apple" in the array:

const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.indexOf("Apple") + 1;

Try It Yourself

Syntax

array.indexOf(item, start)
item Required. The item to be searched for.
start Optional. The starting position of the search. Negative values will count from the end of the array and search to the end.

Return value:

  • Returns -1 if the item is not found.
  • Returns the first occurrence position if the item appears multiple times.

JavaScript Array lastIndexOf()

lastIndexOf() The method is similar to indexOf() Same, but returns the last occurrence position of the specified element.

Example

Search for the item "Apple" in the array:

const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.lastIndexOf("Apple") + 1;

Try It Yourself

Syntax

array.lastIndexOf(item, start)
item Required. The item to be searched for.
start Optional. The starting position of the search. Negative values will count from the end of the array and search to the beginning.

JavaScript Array includes()

ECMAScript 2016 introduced Array.includes() A method that allows us to check if an element is present in an array (including NaN, unlike indexOf).

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango"); // Returns true

Try It Yourself

Syntax

array.includes(search-item)

search-item:The element to be searched.

Note:includes() can check NaN values, while indexOf() cannot.

Browser Support

includes() is ECMAScript 2016 feature.

Since March 2017, all modern browsers support ES2016:

Chrome Edge Firefox Safari Opera
Chrome 52 Edge 15 Firefox 52 Safari 10.1 Opera 39
July 2016 April 2017 March 2017 May 2017 August 2016

Internet Explorer does not support includes().

JavaScript Array find()

find() The method returns the value of the first array element that passes the test function.

Example

Find the first element greater than 18:

const numbers = [4, 9, 16, 25, 29];
let first = numbers.find(myFunction);
function myFunction(value, index, array) {
  return value > 18;
}

Try It Yourself

Note:The function accepts 3 parameters:

  • Element value
  • Element index
  • The array itself

Browser Support

find() is a feature of ES6 (JavaScript 2015).

Since June 2017, all modern browsers support ES6:

Chrome Edge Firefox Safari Opera
Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
May 2016 April 2017 June 2017 September 2016 June 2016

Internet Explorer does not support find().

JavaScript Array findIndex()

findIndex() The method returns the index of the first array element that passes the test function.

Example

Find the index of the first element greater than 18:

const numbers = [4, 9, 16, 25, 29];
let first = numbers.findIndex(myFunction);
function myFunction(value, index, array) {
  return value > 18;
}

Try It Yourself

Note:The function accepts 3 parameters:

  • Element value
  • Element index
  • The array itself

Browser Support

findIndex() is a feature of ES6 (JavaScript 2015).

Since June 2017, all modern browsers support ES6:

Chrome Edge Firefox Safari Opera
Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
May 2016 April 2017 June 2017 September 2016 June 2016

Internet Explorer does not support findIndex().

JavaScript Array findLast()

ES2023 has added findLast() A method that starts searching from the end of the array and returns the value of the first element that meets the condition.

Example

const temp = [27, 28, 30, 40, 42, 35, 30];
let high = temp.findLast(x => x > 40);

Try It Yourself

Browser Support

findLast() Is a feature of ES2023.

Starting from July 2023, all modern browsers support:

Chrome Edge Firefox Safari Opera
Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
February 2023 February 2023 July 2023 March 2023 May 2023

JavaScript Array findLastIndex() Method

findLastIndex() Method finds the index of the last element that meets the condition.

Example

const temp = [27, 28, 30, 40, 42, 35, 30];
let pos = temp.findLastIndex(x => x > 40);

Try It Yourself

Browser Support

findLastIndex() Is a feature of ES2023.

Starting from July 2023, all modern browsers support:

Chrome Edge Firefox Safari Opera
Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
February 2023 February 2023 July 2023 March 2023 May 2023

Complete Array Reference Manual

For a complete array reference, please visit:JavaScript Array Reference Manual.

This manual includes descriptions and examples of all array properties and methods.