JavaScript | typedArray.every() with Examples

The typedArray.every() function is an inbuilt function in JavaScript which is used to test whether the elements present in the typedArray satisfies the condition provided by a function.
Syntax:

typedarray.every(callback)

Parameters: It takes callback function as 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 elements present in the typedArray otherwise returns false.

    JavaScript code to show the working of this function:

    Code #1:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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
      document.write(A.every(is_negative));
        
    </script>

    chevron_right

    
    

    Output:

    true

    Code #2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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
      document.write(A.every(is_positive));
        
    </script>

    chevron_right

    
    

    Output:

    false


    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.