JavaScript(JS) array method - includes

‮tth‬ps://www.theitroad.com

The includes() method is an array method in JavaScript that determines whether an array includes a certain value among its entries. It returns true if the array contains the specified value, and false otherwise. The method uses strict equality (===) to compare the value with elements in the array.

Here's the syntax of the includes() method:

array.includes(valueToFind[, fromIndex]);
  • array is the array that the includes() method is called on.
  • valueToFind is the value to search for in the array.
  • fromIndex is the index to start searching from. It is an optional parameter, and if not specified, the search starts from index 0.

The includes() method returns a boolean value (true or false).

Here's an example of how to use the includes() method:

let array = [1, 2, 3, 4, 5];

console.log(array.includes(3)); // true
console.log(array.includes(6)); // false

In this example, we have an array array containing the elements [1, 2, 3, 4, 5]. We use the includes() method to check whether the array contains the value 3 and 6. The output of this code will be:

true
false

Note that the includes() method was introduced in ECMAScript 2016, so it may not be supported by older browsers. If you need to achieve the same functionality in older browsers, you can use the indexOf() method.