Skip to content

JavaScript Array findIndex() | Method

  • by

JavaScript Array findIndex() Method is used to get the index of the first element in an array. It returns the index of the first element in an array if it satisfied the provided testing function else returns -1.

array.findIndex(function(currentValue, index, arr), thisValue)

JavaScript Array findIndex

A simple example code returns the index of the first element that passes a test.

<!DOCTYPE html>
<html>
<body>

  <script>
    const ages = [3, 10, 18, 20];

    console.log(ages.findIndex(checkAge));

    function checkAge(age) {
      return age > 18;
    }
  </script>
</body>
</html>

Output:

JavaScript Array findIndex() Method

Arrow function

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber)); // 3

More examples

// function that returns even number
function isEven(element) {
  return element % 2 == 0;
}

// defining an array of integers
let numbers = [1, 45, 8, 98, 7];

// returns the index of the first even number in the array
let firstEven = numbers.findIndex(isEven);


console.log(firstEven); // 2

Do comment if you have any doubts or suggestions on this Js Array method.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading