Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript typedArray.every() with Examples

JavaScript typedArray.every() with Examples

The Javascript typedArray.every() function is an inbuilt function in JavaScript that is used to test whether the elements present in the typedArray satisfy the condition provided by a function. 

Syntax:

typedarray.every(callback)

Parameters: It takes the callback function as a parameter. The callback is a function for testing the elements of the typedArray. This callback function takes three parameters that are specified below-

  • current_value: It is the current element being processed in the typedArray.
  • index: It is the index of the current element which is being processed in the typed array.
  • array: It is the typedArray.

Return value: It returns true if the function callback returns a true value for each and every array element present in the typedArray otherwise returns false.

JavaScript examples to show the working of this function:

Example 1: In this example, the typedarray.every(callback) function satisfies the conditions and hence returns true.

javascript




<script>
    // is_negative function is called to test the
    // elements of the typedArray element.
    function is_negative(current_value, index, array)
    {
        return current_value < 0;
    }
      
    // Creating a typedArray with some elements
    const A = new Int8Array([ -5, -10, -15, -20, -25, -30 ]);
      
    // Printing whether elements are satisfied by the
    // functions or not
    console.log(A.every(is_negative));
</script>


Output:

true

Example 2:  In this example, the typedarray.every(callback) function does not satisfy the conditions and hence returns false.

javascript




<script>
    // is_negative function is called to test the
    // elements of the typedArray element.
    function is_positive(current_value, index, array)
    {
        return current_value > 0;
    }
      
    // Creating a typedArray with some elements
    const A = new Int8Array([ -5, -10, -15, -20, -25, -30 ]);
      
    // Printing whether elements are satisfied by the
    // functions or not
    console.log(A.every(is_positive));
</script>


Output:

false

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

Most Popular

Recent Comments