
The Array.prototype.includes()
method in JavaScript checks whether an array contains a specific value and returns a boolean value indicating whether the value is present or not.
Syntax:
array.includes(valueToFind[, fromIndex]);
- valueToFind – The value to search for in the array.
- fromIndex (Optional) – The index from which to start searching for the value in the array. If the value is not found after the specified index, the method returns
false
. If omitted, the default value is0
.
Return value:
true
if the array contains the value,false
otherwise.
Example:
const array1 = [1, 2, 3, 4, 5];
console.log(array1.includes(2)); // true
console.log(array1.includes(6)); // false
console.log(array1.includes(4, 3)); // true
console.log(array1.includes(4, 4)); // false
In the above example, array array1
is checked whether 2
, 6
, 4
is present in it or not, with and without fromIndex
.