JavaScript includes()
method is used to determine whether an array contains a certain value or not and returns a boolean value indicating the result.
JavaScript contains an array example
Simple example code determines whether an array includes a certain value or not.
<!DOCTYPE html>
<html>
<body>
<script>
const myArray = [1, 2, 3, 4, 5];
console.log(myArray.includes(3));
console.log(myArray.includes(6));
</script>
</body>
</html>
Output:
The includes()
method takes an optional second parameter, which is the index to start the search from. For example:
const myArray = [1, 2, 3, 4, 5];
console.log(myArray.includes(2, 2)); // Output: false
console.log(myArray.includes(4, 3)); // Output: true
Comment if you have any doubts or suggestions on this JS array topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version