Open In App

JavaScript typedArray.find() with Example

Last Updated : 26 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Javascript typedArray.find() is an inbuilt function in JavaScript that is used to return a value in the typedArray, if the value satisfies the condition given in the function, otherwise, it returns undefined. 

Syntax:

typedArray.find(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 a value of the array if the elements satisfy the condition provided by the function, otherwise, it returns undefined. 

JavaScript example to show the working of this function: 

Example: This example shows the basic use of the typedArray.find() 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 find() function to check condition
    // provided by its parameter
    const a = A.find(isNegative);
    const b = B.find(isNegative);
    const c = C.find(isNegative);
    const d = D.find(isNegative);
     
    // Printing the find typedArray
    console.log(a);
    console.log(b);
    console.log(c);
    console.log(d);
</script>


Output:

-10
-30
-10
undefined

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads