JS-058

Beginner4 min~4 min read#array#includes#search
includes()
Check whether an element exists
Goal
Learn to check whether an element exists in an array using includes().
Explanation
- includes() checks whether an array contains a value.
- If the value is found, it returns true.
- If the value is absent, it returns false.
- The method does not return the element's position.
- includes() is very convenient for quick checks.
Analogy
You look in the refrigerator to check whether there is any milk. There are only two possible answers: yes or no.
Code
const fruits = [
"Apple",
"Banana",
"Orange"
];
console.log(fruits.includes("Banana"));
console.log(fruits.includes("Mango"));Result: true false
Common mistake
Beginners sometimes expect to get the element's position. Use indexOf() for that.
Remember
- ✦ includes() returns true or false.
- ✦ It is used to check whether a value exists.
- ✦ It does not return the element's index.
Quick Quiz
What does ["Apple", "Banana"].includes("Banana") return?