TypedArray findIndex() JavaScript

The Javascript TypedArray findIndex() method retrieves the index of the first element that complete the given test in the array. It will return -1, if no element satisfy the given condition.

Syntax:

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

Parameters:
value: It represents the current element’s value.
index: It represents the index position of current element.
arr: It represents the array.
thisValue: It represents this argument for the function.

Returns:
It returns index of the first element that satisfy the given test in the array. It will return -1, if no element satisfy the given condition.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">  
function func(i)  
{ return i>= "O";  }
var Jewels = ["DIAMOND","GOLD","PLATINUM","SILVER"];  
var gems =Jewels.findIndex(func);   
document.write(gems);  
</script>
</body>
</html>

Output:

2

Example 2:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">  
function isNegative(element, index, array) {
  return element < 0;
}
 
const testArray = new Int8Array([45, -26, 56, -23, 78]);
 
document.write(testArray.findIndex(isNegative));
 
</script>
</body>
</html>

Output:

1
Please Share