• JavaScript Video Tutorials

JavaScript - TypedArray every() Method



The JavaScript TypeArray every() method is used to determine whether all the elements in a typed array pass a given test. This test is implemented by a function called callbackFn. If all elements in the typed array pass the test, then the method returns a boolean true. If any element fails the test, then the method returns false.

Here are some additional points you should know about the 'every()' method −

  • The every() method operates on a TypedArray (such as Uint8Array, Int16Array, etc.).

  • It accepts a testing function as a parameter.

  • The testing function is executed for each element in the TypedArray.

  • If an element satisfies the condition specified by the testing function (it returns a true value).

Syntax

Following is the syntax of JavaScript TypedArray every() method −

every(callbackFn, thisArg)

Parameters

This method accepts two parameters named 'callbackFn' and 'thisArg', which are described below −

callbackFn − This parameter is a testing function that will be executed for each element in the TypedArray.

This function takes three parameters named 'element', 'index', and 'array'. Here's the description for each parameter −

  • element − Represents the current element being processed in the TypedArray.

  • index − Indicates the index (position) of the current element within the TypedArray.

  • array − Refers to the entire TypedArray.

thisArg (optional) − This is an optional parameter that allows you to specify the value of this within the callbackFn.

Return value

This method returns true if all elements in the typed array pass the test implemented by the callback function, and false otherwise.

Examples

Example 1

If every elements in the typed array fail the callbackFn test, it returns false.

In the following example, we use the JavaScript TypedArray every() method to check whether all elements of this typed array [1, 2, 3, 4, 5, 6, 7, 8] pass the test implemented by the callback function named isEven(). The function checks for the even or odd values.

<html>
<head>
   <title>JavaScript TypedArray every() Method</title>
</head>
<body>
   <script>
      function isEven(element, index, array){
         return element %2 == 0;
      }
      const T_array = new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]);
      document.write("The typedArray elements are: ", T_array);
      document.write("<br>Are all the elements in the typed array even? ", T_array.every(isEven));
   </script>    
</body>
</html>

Output

The above program returns 'false'.

The typedArray elements are: 1,2,3,4,5,6,7,8
Are all the elements in the typed array even? false

Example 2

If every elements in the typed array pass the callbackFn test, it returns true.

Here’s another example of the JavaScript TypedArray every() method. We use this method to check whether all elements of a typed [−1, −2, −3, −4, −5, −6, −7, −8] array pass the test provided by a function named isNegative(). The function determines whether a value is negative, and we pass this function as an argument to this method.

<html>
<head>
   <title>JavaScript TypedArray every() Method</title>
</head>
<body>
   <script>
      function isNegative(element, index, array){
         return element < 0;
      }
      const T_array = new Int8Array([-1, -2, -3, -4, -5, -6, -7, -8]);
      document.write("The typedArray elements are : ", T_array);
      document.write("<br>Are all the elements in the typed array negative ? ", T_array.every(isNegative));
   </script>    
</body>
</html>

Output

After executing the above program, it returns 'true'.

The typedArray elements are : -1,-2,-3,-4,-5,-6,-7,-8
Are all the elements in the typed array negative ? true
Advertisements