JavaScript includes()
The includes()
method in JavaScript is used to determine whether an array or a string includes a certain value. It returns a boolean value indicating whether the value is found or not. The method is case-sensitive for strings.
Syntax
The syntax for includes()
method is as follows:
array.includes(valueToFind, fromIndex)
Here,
valueToFind
is the value to be searched in the array.fromIndex
(optional) is the index at which to begin searching in the array. If not specified, it defaults to zero.
Example
const numbers = [1, 2, 3, 4, 5];
console.log(numbers.includes(2)); //true
console.log(numbers.includes(6)); //false
const message = "Hello, World!";
console.log(message.includes("Hello")); //true
console.log(message.includes("hello")); //false
Output
The output of the above code will be:
true
false
true
false