• JavaScript Video Tutorials

JavaScript - TypedArray includes() Method



The JavaScript TypedArray includes() method is used to determine whether a searchElement value is present in the entire typed array or within the range from fromIndex (the starting position) to the end of the typed array. This method returns a boolean value 'true' if the specified searchElement is found within the typed array. If the searchElement is not found, it returns 'false'.

Syntax

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

includes(searchElement, fromIndex)

Parameters

This method accepts two parameters named 'searchElement' and 'fromIndex', which are described below −

  • searchElement − The element to be searched in the typed array.

  • fromIndex − The zero-based index at which start searching the searchElement.

Return value

This method returns 'true' if searchElement is found in the typed array; 'false' otherwise.

Examples

Example 1

If the specified searchElement is not found within the typed array, it will return 'false'.

In the following example, we are using the JavaScript TypedArray includes() method to determine whether the specified searchElement 20 is found in this typed array [10, 40, 30, 50, 80, 100].

<html>
<head>
   <title>JavaScript TypedArray includes() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([10, 40, 30, 50, 80, 100]);
      document.write("The typed array elements are: ", T_array);
      const searchElement = 20;
      document.write("<br>The elenent to be searched: ", searchElement);
      
      //using the includes() method
      document.write("<br>Does element ", searchElement, " is found in typed array ? ",  T_array.includes(searchElement));
   </script>    
</body>
</html>

Output

The above programs returns 'false'.

The typed array elements are: 10,40,30,50,80,100
The elenent to be searched: 20
Does element 20 is found in typed array ? false

Example 2

If the specified searchElement is found within the typed array, it will return 'true'.

The following is another example of the JavaScript TypedArray includes() method. We use this method to determine whether the specified searchElement 4 is found in this typed array [1, 2, 3, 4, 5, 6, 7, 8].

<html>
<head>
   <title>JavaScript TypedArray includes() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
      document.write("The typed array elements are: ", T_array);
      const searchElement = 4;
      document.write("<br>The elenent to be searched: ", searchElement);
      
      //using the includes() method
      document.write("<br>Does element ", searchElement, " is found in typed array ? ",  T_array.includes(searchElement));
   </script>    
</body>
</html>

Output

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

The typed array elements are: 1,2,3,4,5,6,7,8
The elenent to be searched: 4
Does element 4 is found in typed array ? true

Example 3

If both 'searchElement' and 'fromIndex' parameters are passed to this method, it starts searching for the 'searchElement' from the specified 'fromIndex' in the typed array.

In this program, we use the includes() method to determine whether the specified searchElement 2 is found between the starting position 4, and till the end of this typed array [1, 2, 3, 5, 7, 9, 6, 8, 11, 15].

<html>
<head>
   <title>JavaScript TypedArray includes() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([1, 2, 3, 5, 7, 9, 6, 8, 11, 15]);
      document.write("The typed array elements are: ", T_array);
      const searchElement = 2;
      const fromIndex = 3;
      document.write("<br>The elenent to be searched: ", searchElement);
      document.write("<br>The starting position: ", fromIndex);
      
      //using the includes() method
      document.write("<br>Does element ", searchElement, " is found between start index ", 
      fromIndex, " and till the end of typed array ? ",  T_array.includes(searchElement, fromIndex));
   </script>    
</body>
</html>

Output

Once the above program is executed, it will return 'false'.

The typed array elements are: 1,2,3,5,7,9,6,8,11,15
The elenent to be searched: 2
The starting position: 3
Does element 2 is found between start index 3 and till the end of typed array ? false
Advertisements