JavaScript TypedArray.prototype.includes() Method

In JavaScript the TypedArray.prototype.includes() method is used to check that the given typed array contains a specific given element or not, if it contains the element it returns true otherwise false.

Syntax

typedArray.includes(searchElement)
OR
typedArray.includes(searchElement, fromIndex)

Parameters

  • searchElement: The value to search for.
  • fromIndex: (Optional) The position in the array at which searching begins. If the value not given the default position will be considered as 0.

Return Value

If the element is found then it will return true otherwise it will return false.

Example 1: In the given below example we are checking the presence of an element in Unit8Array.

JavaScript
// Example usage of includes() 
// to check for presence of an element
const uint8Array = new Uint8Array([10, 20, 30, 40, 50]);

const contains30 = uint8Array.includes(30);
const contains60 = uint8Array.includes(60);

console.log(contains30); // Output: true
console.log(contains60); // Output: false

Output
true
false

Example 2: In the given below example we are checking for the presence of a floating-point number in Float32Array.

JavaScript
// Example usage of includes() to check
// for presence of a floating-point number
const float32Array = new Float32Array([0.1, 2.5, 3.7, 4.9, 5.5]);

const contains2Point5 = float32Array.includes(2.5);
const contains6Point5 = float32Array.includes(6.5);

console.log(contains2Point5); 
console.log(contains6Point5); 

Output
true
false

Contact Us