You can use the Array.prototype.includes()
method to check if an array contains a specific string in JavaScript. Here’s an example:
let arr = ['apple', 'banana', 'cherry'];
let search = 'banana';
if (arr.includes(search)) {
console.log(`The array contains the string "${search}"`);
} else {
console.log(`The array does not contain the string "${search}"`);
}
This will output The array contains the string "banana"
.
+ There are no comments
Add yours