Includes

Checking whether a specific value exists inside an array is a common task. In JavaScript, you can use the built-in .includes() method instead of writing manual search loops.

Arrays

The .includes() method returns a boolean indicating whether the exact value is found inside the array.

const fruits = ["apple", "orange", "banana"];

console.log(fruits.includes("orange")); // Output: true
console.log(fruits.includes("pear"));   // Output: false

Strings

JavaScript Strings also feature an identical .includes() method to check for substrings! This search is strictly case-sensitive.

const greeting = "Hello, world!";

console.log(greeting.includes("world"));  // Output: true
console.log(greeting.includes("banana")); // Output: false

// Case sensitivity matters!
console.log(greeting.includes("World"));  // Output: false