Curriculum
indexOf() and includes() Methods in JavaScript are important array methods used to search elements inside arrays. Understanding indexOf() and includes() Methods is essential for beginners because these methods help developers validate data, search values, filter content, and build dynamic JavaScript applications efficiently.
Arrays often need operations like:
JavaScript provides built-in methods for searching:
indexOf()includes()These methods help developers:
They are widely used in:
Understanding indexOf() and includes() Methods is essential for efficient array operations.
These methods help developers:
Modern JavaScript applications frequently use these methods.
The indexOf() method:
If the element does not exist:
-1Example:
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.indexOf("Banana"));
Output:
1
Because:
"Banana" exists at index 1Basic syntax:
arrayName.indexOf(value);
Example:
let colors = ["Red", "Blue", "Green"];
console.log(colors.indexOf("Green"));
Output:
2
The method searches from:
Example:
let numbers = [10, 20, 30];
console.log(numbers.indexOf(50));
Output:
-1
Because:
50 does not exist in the arrayDevelopers commonly use:
-1Example:
let users = ["Rahul", "Aman", "Priya"];
if(users.indexOf("Aman") !== -1){
console.log("User Found");
}
Output:
User Found
This is useful for validations.
The includes() method:
It returns:
truefalseExample:
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.includes("Mango"));
Output:
true
Because:
"Mango" existsBasic syntax:
arrayName.includes(value);
Example:
let colors = ["Red", "Blue", "Green"];
console.log(colors.includes("Black"));
Output:
false
Because:
"Black" does not exist| Method | Return Value |
|---|---|
indexOf() |
Index or -1 |
includes() |
true or false |
Both methods help search arrays efficiently.
Example:
let products = ["Laptop", "Mobile", "Tablet"];
if(products.includes("Mobile")){
console.log("Product Available");
}
Output:
Product Available
This improves readability compared to indexOf().
Both methods support starting positions.
let numbers = [10, 20, 10, 30];
console.log(numbers.indexOf(10, 1));
Output:
2
Search starts from index 1.
let numbers = [10, 20, 30];
console.log(numbers.includes(20, 2));
Output:
false
Search begins from index 2.
These methods are used in:
Modern applications constantly search datasets dynamically.
Example:
let users = ["Rahul", "Aman"];
if(!users.includes("Priya")){
users.push("Priya");
}
console.log(users);
Output:
["Rahul", "Aman", "Priya"]
This prevents duplicate entries.
Beginners often:
indexOf() returns -1Incorrect example:
if(numbers.indexOf(20)){
Problem:
0 becomes falseCorrect example:
if(numbers.indexOf(20) !== -1){
Benefits include:
These methods simplify array searching operations.
Best practices include:
includes() for readabilityindexOf() when index position is neededReadable search logic improves maintainability.
Understanding indexOf() and includes() Methods helps developers:
These methods are fundamental in modern JavaScript development.
indexOf() and includes() Methods in JavaScript are array methods used to search elements and validate their existence inside arrays. They are widely used in filtering, validation, search systems, shopping carts, and modern dynamic applications.
The indexOf() method returns the index position of an element.
The includes() method checks whether an element exists inside an array.
It returns -1.
includes() is generally more readable for checking existence.
They are used in validation systems, searches, filtering, and dynamic web applications.
WhatsApp us