YouTip LogoYouTip

Jsref Findindex

# JavaScript findIndex() Method [![Image 3: Array Object Reference Manual](#) JavaScript Array Object](#) ## Example Get the index position of the first element in the array where the age is greater than or equal to 18. ```javascript var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; } function myFunction() { document.getElementById("demo").innerHTML = ages.findIndex(checkAdult); } _fruits_ Output: 2 [Try it yourself Β»](#) * * * ## Definition and Usage The findIndex() method returns the index position of the first element in the array that passes a test (provided as a function). The findIndex() method executes the function once for each element present in the array: * If it finds an array element where the function returns a true value, findIndex() returns the index of that array element and does not execute the function further. * If no matching elements are found, it returns -1. **Note:** findIndex() does not execute the function for empty arrays. **Note:** findIndex() does not change the original array. * * * ## Browser Support The numbers in the table specify the first browser version that fully supports the method. | Method | | | | | | | --- | --- | --- | --- | --- | --- | | findIndex() | 45.0 | 12.0 | 25.0 | 7.1 | 32.0 | **Note:** IE 11 and earlier versions do not support the findIndex() method. * * * ## Syntax ```javascript array.findIndex(function(currentValue, index, arr), thisValue) ## Parameters | Parameter | Description | | --- | --- | | _function(currentValue, index,arr)_ | Required. A function to be run for each element in the array.
Function arguments:
Parameter | Description | | --- | --- | | _currentValue_ | Required. The current element | | _index_ | Optional. The index of the current element | | _arr_ | Optional. The array object the current element belongs to | | | _thisValue_ | Optional. A value to be passed to the function to be used as its "this" value.
If this parameter is empty, "undefined" will be passed as the "this" value. | ## Technical Details | Return Value: | Returns the index of the first array element that passes the test. If no elements pass the test, it returns -1. | | --- | | JavaScript Version: | ECMAScript 6 | ## More Examples ## Example Return the array index of the element that is greater than the number entered in the input field: ```javascript var ages = [4, 12, 16, 20]; function checkAdult(age) { return age >= document.getElementById("ageToCheck").value; } function myFunction() { document.getElementById("demo").innerHTML = ages.findIndex(checkAdult); } [Try it yourself Β»](#) * * JavaScript Array Object](#)
← Jsref ForeachJsref Copywithin β†’