• JavaScript Video Tutorials

JavaScript - TypedArray some() Method



The JavaScript TypedArray some() method is used to check whether at least a single element in a typed array passes the test implemented by the provided function. The method returns a boolean value 'true' if at least one element passes the test, and returns 'false' otherwise. It is important to note that this method does not modify the original typed array but instead returns a new typed array.

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

  • The some() 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 some() method −

some(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 atlesat one element in typed array pass the test implemented by provided function; false otherwise.

Examples

Example 1

If at least one element in the typed array passes the callbackFn function test, this method will return true.

In the following example, we use the JavaScript TypedArray some() method to determine whether at least one element in the typed array [1, 2, 3, 4, 5, 6, 7] passes the test implemented by the isEven() function. This function checks for the even number in a typed array, and we pass this function as an argument to some() method.

<html>
<head>
   <title>JavaScript TypedArray some() 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]);
      document.write("The typed array elements are: ", T_array);
      
      //using some() method
      document.write("<br>Is this typed array ", T_array, " contain at least one even number? ", T_array.some(isEven));
   </script>    
</body>
</html>

Output

The above program returns 'true'.

The typed array elements are: 1,2,3,4,5,6,7
Is this typed array 1,2,3,4,5,6,7 contain at least one even number? true

Example 2

If not a single element in the typed array passes the callbackFn function test, the some() method will return false.

The following is another example of the JavaScript TypedArray some() function. We are using this method to check whether at least one element passes the test provided the isNegative() function. We pass this function as an argument to this method, it checks for the negative number in the typed array.

<html>
<head>
   <title>JavaScript TypedArray some() Method</title>
</head>
<body>
   <script>
      function isNegative(element, index, array){
         return element < 0;
      }
      const T_array = new Int8Array([10, 20, 30, 40, 50, 60, 70, 80]);
      document.write("The typed array elements are: ", T_array);
      
      //using some() method
      document.write("<br>Is this typed array ", T_array, " contain at least one negative number? ", T_array.some(isNegative));
   </script>    
</body>
</html>

Output

After executing the above program, it will return 'false'.

The typed array elements are: 10,20,30,40,50,60,70,80
Is this typed array 10,20,30,40,50,60,70,80 contain at least one negative number? false
Advertisements