The `includes` Method
The includes method can be used to quickly determine of a specific element exists in the array or not.
Following is the general syntax of the includes method:
arrayName.includes(element);
The includes method returns a boolean (true or false) value based on whether or not the element exists in the array.
- Returns
trueif the element is found; - Returns
falseif the element is not found.
1234let itemsInStock = ["Laptop", "Phone", "Tablet", "Charger"]; console.log(itemsInStock.includes("Phone")); console.log(itemsInStock.includes("Headphones"));
The includes method performs a strict comparison (===), meaning it checks for exact matches, including case sensitivity. For example, itemsInStock.includes("phone") would return false because "phone" (lowercase) is not the same as "Phone" (uppercase "P").
1. What will be the output of the following code?
2. Which of the following statements is true about the includes method?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the includes method works with numbers or other data types?
What happens if the array contains objects instead of simple values?
Can you show how to use includes in an if statement for conditional logic?
Awesome!
Completion rate improved to 1.33
The `includes` Method
Swipe to show menu
The includes method can be used to quickly determine of a specific element exists in the array or not.
Following is the general syntax of the includes method:
arrayName.includes(element);
The includes method returns a boolean (true or false) value based on whether or not the element exists in the array.
- Returns
trueif the element is found; - Returns
falseif the element is not found.
1234let itemsInStock = ["Laptop", "Phone", "Tablet", "Charger"]; console.log(itemsInStock.includes("Phone")); console.log(itemsInStock.includes("Headphones"));
The includes method performs a strict comparison (===), meaning it checks for exact matches, including case sensitivity. For example, itemsInStock.includes("phone") would return false because "phone" (lowercase) is not the same as "Phone" (uppercase "P").
1. What will be the output of the following code?
2. Which of the following statements is true about the includes method?
Thanks for your feedback!