HomeLanguagesJavascriptJavaScript typedArray.findIndex() with Example

JavaScript typedArray.findIndex() with Example

The Javascript typedArray.findIndex() is an inbuilt function in JavaScript that 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 that is 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 example to show the working of this function: 

Example: This example shows the basic use of the typedArray.findIndex() method in Javascript.

javascript




<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
    console.log(a);
    console.log(b);
    console.log(c);
    console.log(d);
</script>


Output:

0
2
0
-1
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32713 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7088 POSTS0 COMMENTS
Nicole Veronica
12228 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12310 POSTS0 COMMENTS
Shaida Kate Naidoo
7203 POSTS0 COMMENTS
Ted Musemwa
7470 POSTS0 COMMENTS
Thapelo Manthata
7189 POSTS0 COMMENTS
Umr Jansen
7171 POSTS0 COMMENTS