What is the use of Array.findIndex() method in JavaScript?

JavascriptObject Oriented ProgrammingProgramming


Array.findIndex() returns the first index of the array element that pass a test implemented by the provided function. This method executes the function once for each element that is present in array.If once the condition of the function satisfied, Index of the first element that satisfied the condition will be returned, if not value '-1 ' will be returned.

               Once an element satisfies the provided condition then findIndex() does't check other values.In the following example findIndex() method check whether the salary elements are greater than the given salary 15000.Since the first element to satisfy the condition is 17000 it won't check the other values such as 28000 and 30000 and returns the index of 17000.


 Live Demo

<p id="findindex"></p>
   var wages = [6000, 10000, 17000, 28000, 30000];
   function checkSal(wage) {
      return wage >= 15000;
   document.getElementById("findindex").innerHTML = wages.findIndex(checkSal);


Published on 28-May-2019 11:17:00