ES2016 Specifications included the includes() method for Array data structure.
The includes() method check if an array includes a certain element, returning true or false as appropriate.
Syntax
array.includes(searchElement);
Example
const array = ["Red", "Orange", "Yellow", "Olive", "Green"];
if (array.includes("Red")) {
console.log("Red was found in the array"); // true
} else {
console.log("Red was not found in the array"); // false
}
Note: The includes() method is case sensitive.
Hope you find this helpful.
Related Articles
Deepen your understanding with these curated continuations.
Common Ways to Create Objects in JavaScript
Discover effective ways to create objects in JavaScript, including constructors, ES6 classes, and singletons. Master object creation with this detailed guide.
Convert array to an object in JavaScript
This article explains simplest and quickest way to convert array to an object in JavaScript. Using widely accepted spread operator `...` makes easy to do it.
How to Update an Array Element in JavaScript
Learn the simplest ways to update array elements in JavaScript. This guide explains how to use assignment operators and modern methods to modify array values.