Open Playground
JS TutorialLesson #69

JavaScript Array Search

[Complete JavaScript Array Reference](jsarrayreference.asp) ## JavaScript Array indexOf() The indexOf() method searches an array for an element value and returns its position.

Array Search Methods

Complete JavaScript Array Reference ## JavaScript Array indexOf() The indexOf() method searches an array for an element value and returns its position.

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

Example

Example Code

javascript
Try it Yourself
const fruits = ["Apple", "Orange", "Apple", "Mango"];

let position = fruits.indexOf("Apple") + 1;

Syntax

array.indexOf(item, start)

itemRequired. The item to search for.
startOptional. Where to start the search. Negative values will start at the given position counting from the end, and search to the end.

Array.indexOf() returns -1 if the item is not found.

If the item is present more than once, it returns the position of the first occurrence.


JavaScript Array lastIndexOf()

Array.lastIndexOf() is the same as Array.indexOf(), but returns the position of the last occurrence of the specified element.

Example

Example Code

javascript
Try it Yourself
const fruits = ["Apple", "Orange", "Apple", "Mango"];

let position = fruits.lastIndexOf("Apple") + 1;

Syntax

array.lastIndexOf(item, start)

itemRequired. The item to search for
startOptional. Where to start the search. Negative values will start at the given position counting from the end, and search to the beginning

JavaScript Array includes()

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

Example

Example Code

javascript
Try it Yourself
const fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.includes("Mango"); // is true

Syntax

array.includes(search-item)

Array.includes() allows to check for NaN values. Unlike Array.indexOf().

JavaScript Array find()

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

This example finds (returns the value of) the first element that is larger than 18:

Example

Example Code

javascript
Try it Yourself
const numbers = [4, 9, 16, 25, 29];

let first =
  numbers.find(myFunction);

function myFunction(value, index, array) {
  return
  value > 18;
}

Note that the function takes 3 arguments:

  • The item value
  • The item index
  • The array itself

JavaScript Array findIndex()

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

This example finds the index of the first element that is larger than 18:

Example

Example Code

javascript
Try it Yourself
const numbers = [4, 9, 16, 25, 29];

let first =
  numbers.findIndex(myFunction);

function myFunction(value, index, array) {

    return
  value > 18;
}

Note that the function takes 3 arguments:

  • The item value
  • The item index
  • The array itself

JavaScript Array findLast() Method

ES2023 added the findLast() method that will start from the end of an array and return the value of the first element that satisfies a condition.

Example

Example Code

javascript
Try it Yourself
const temp = [27, 28, 30, 40, 42, 35, 30];

let high = temp.findLast(x => x > 40);

Browser Support

findLast() is an ECMAScript 2023 feature.

JavaScript 2023 is supported in all modern browsers since July 2023:

Chrome 110Edge 110Firefox 115Safari 16.4Opera 96
Feb 2023Feb 2023Jul 2023Mar 2023May 2023

JavaScript Array findLastIndex() Method

The findLastIndex() method finds the index of the last element that satisfies a condition.

Example

Example Code

javascript
Try it Yourself
const temp = [27, 28, 30, 40, 42, 35, 30];

let pos = temp.findLastIndex(x => x > 40);

Browser Support

findLastIndex() is an ECMAScript 2023 feature.

JavaScript 2023 is supported in all modern browsers since July 2023:

Chrome 110Edge 110Firefox 115Safari 16.4Opera 96
Feb 2023Feb 2023Jul 2023Mar 2023May 2023
See Also: > Array Tutorial > Array Constructor > Array Basic Methods > Array Sort Methods > Array Iteration Methods > Array Reference

Complete JavaScript Reference > For a complete reference to all JavaScript properties and methods, with full descriptions and many examples, go to: > EA Academy' Full JavaScript Reference. > The reference inludes all JavaScript updates from 1999 to 2025.


Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const fruits = ["Apple", "Orange", "Apple", "Mango"];

let position = fruits.indexOf("Apple") + 1;

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const fruits = ["Apple", "Orange", "Apple", "Mango"];

let position = fruits.lastIndexOf("Apple") + 1;

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const fruits = ["Banana", "Orange", "Apple", "Mango"];


fruits.includes("Mango"); // is true

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const numbers = [4, 9, 16, 25, 29];

let first = 
  numbers.find(myFunction);

function myFunction(value, index, array) {
  return 
  value > 18;
}

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const numbers = [4, 9, 16, 25, 29];

let first = 
  numbers.findIndex(myFunction);

function myFunction(value, index, array) {

    return 
  value > 18;
}

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const temp = [27, 28, 30, 40, 42, 35, 30];

let high = temp.findLast(x => x > 40);