JavaScript | typedArray.findIndex() with Example

The typedArray.findIndex() is an inbuilt function in JavaScript which is used to return an index in the tyedArray if the value satisfies the condition given in the function, otherwise, it returns -1.
Syntax:

typedarray.findIndex(callback)

Parameters: It takes the parameter “callback” function which checks each element of the typedArray satisfied by the condition provided. The Callback function takes three parameters that are specified below-

  • element: It is the value of the element.
  • index: It is the index of the element.
  • array: It is the array which are being traversed.
  • Return value: It returns an index in the array if the elements satisfied the condition provided by the function, otherwise, it returns -1.
    JavaScript code to show the working of this function:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
       // Calling isNegative function to check
       // elements of the typedArray
       function isNegative(element, index, array)
       {
        return element < 0;
       }
      
       // Created some typedArrays.
       const A = new Int8Array([ -10, 20, -30, 40, -50 ]);
       const B = new Int8Array([ 10, 20, -30, 40, -50 ]);
       const C = new Int8Array([ -10, 20, -30, 40, 50 ]);
       const D = new Int8Array([ 10, 20, 30, 40, 50 ]);
      
       // Calling findIndex() function to check condition
       // provided by its parameter
       const a = A.findIndex(isNegative);
       const b = B.findIndex(isNegative);
       const c = C.findIndex(isNegative);
       const d = D.findIndex(isNegative);
      
       // Printing the indexes typedArray
       document.write(a +"<br>");
       document.write(b +"<br>");
       document.write(c +"<br>");
       document.write(d);
      
    </script>

    chevron_right

    
    

    Output:

    0
    2
    0
    -1


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.