grelevel.blogg.se

Findindex array javascript
Findindex array javascript







findindex array javascript

In this tutorial, you have learned array findIndex() method in JavaScript with the help of example programs. The general syntax of the findIndex() method in JavaScript is: // Arrow functionĪrr.findIndex((element) => ) => name = 'Belly') Let index = numbers.findIndex(isGreater) Ĭonsole.log("Index of first greater number than 2: " +index) Find the index of first greater number than 2. Callback function to check greater number than 2.

findindex array javascript

Otherwise, it returns -1 if no matching is found.įor example, the following snippet code returns the index of the first number that’s greater than 2. In other words, find() method of array returns the index of first array element that satisfies provided testing function (i.e. The findIndex() method works similarly to the find() method, but it returns the index of the first array element that meets the certain criteria defined in the callback function.









Findindex array javascript