DEV Community

vidhya murali
vidhya murali

Posted on

JavaScript Array Search

JavaScript Array indexOf()

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

JavaScript Array lastIndexOf()

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

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).

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:

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:

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.

JavaScript Array findLastIndex() Method

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

Top comments (1)

Collapse
 
karthick_07 profile image
Karthick (k)

good