The findIndex method in JavaScript returns the index of the initial element in an array that meets the specified condition. If no element fulfills the condition, it will return -1.
- The findIndex method does not invoke the callback function for elements in the array that are uninitialized or do not have values.
- The findIndex method does not modify the original array in any way.
Syntax:
array.findIndex(function(Value, index, arr) thisValue)
Parameters:
Value : The value of the current element.
Index : The array index of the current element.
Arr : The array instance that the findIndex function is applied to.
ThisValue: A value designated for the function to serve as its "this" context. In the event that the parameter is left blank, the function will receive "undefined" as its "this" context.
Return value:
The function will return the index of the specified element within the array; if the element is not found, it will yield -1 instead.
Browser Support:
| Chrome | 45.0 |
|---|---|
Edge |
7.1 |
| Firefox | 25.0 |
| Opera | 32.0 |
Example 1
JavaScript TypedArray findIndex Method
<script type="text/javascript">
// JavaScript to illustrate findIndex() method
function Example(value)
{
return value >34;
}
// Input array
var arr = [1,2,3,4,5,6,7,8,9,12,11,14];
var result = arr.findIndex(findFunction);
document.write(result)// expected output: arr[Output:-1]
</script>
Output:
Example 2
JavaScript TypedArray findIndex Method
<script type="text/javascript">
//JavaScript to illustrate findIndex() method
function Example(value)
{
return value ==6;
}
// Input array
var arr = [1,2,3,4,5,6,7,8,9,12,11,14];
var result = arr.findIndex(findFunction);
document.write(result)
// expected output: arr[Output:5]
</script>
Output: