Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript: How to check whether an array includes a particular value or not?
In JavaScript, there are several ways to check whether an array includes a particular value. This article explores different approaches to search for values in arrays effectively.
Array Creation Syntax
let array = [arrayItem1, arrayItem2, arrayItem3, ...];
Using for Loop (Traditional Approach)
The traditional approach involves looping through all array elements and comparing each value with the target value. This method also allows you to find the index of the element.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Array Value Check</title>
</head>
<body>
<h1 style="color: green;">
Welcome To Tutorials Point
</h1>
<script>
let employees_array = [
"steve",
"mark",
"mike",
"bill",
"henry",
"orion"
];
let valueChecker = (value) => {
for (let i = 0; i < employees_array.length; i++) {
let current_value = employees_array[i];
if (value === current_value) {
return value + " is present at index: " + i;
}
}
return value + " is not included in this array..";
};
console.log(valueChecker("mike"));
console.log(valueChecker("casey"));
console.log(valueChecker("henry"));
</script>
</body>
</html>
mike is present at index: 2 casey is not included in this array.. henry is present at index: 4
Using includes() Method (Recommended)
The includes() method is the modern and preferred way to check if an array contains a specific value. It returns true if the value exists, false otherwise.
Syntax
array.includes(searchElement, fromIndex)
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Array includes() Method</title>
</head>
<body>
<h1 style="color: green;">
Welcome To Tutorials Point
</h1>
<script>
let employees_array = [
"steve",
"mark",
"mike",
"bill",
"henry",
"orion"
];
console.log("Is Mark Exists: " + employees_array.includes("mark"));
console.log("Is Orion Exists: " + employees_array.includes("orion"));
console.log("Is Michael Exists: " + employees_array.includes("michael"));
// With starting index
console.log("Is Mike after index 3: " + employees_array.includes("mike", 3));
</script>
</body>
</html>
Is Mark Exists: true Is Orion Exists: true Is Michael Exists: false Is Mike after index 3: false
Other Methods
JavaScript provides additional methods for array searching:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Alternative Array Search Methods</title>
</head>
<body>
<script>
let numbers = [10, 20, 30, 40, 50];
// indexOf() - returns index or -1
console.log("Index of 30:", numbers.indexOf(30));
console.log("Index of 60:", numbers.indexOf(60));
// find() - returns element or undefined
let found = numbers.find(num => num > 25);
console.log("First number greater than 25:", found);
// some() - returns boolean
let hasLargeNumber = numbers.some(num => num > 45);
console.log("Has number greater than 45:", hasLargeNumber);
</script>
</body>
</html>
Index of 30: 2 Index of 60: -1 First number greater than 25: 30 Has number greater than 45: true
Comparison
| Method | Returns | Use Case |
|---|---|---|
includes() |
boolean | Check if value exists |
indexOf() |
index or -1 | Find position of value |
find() |
element or undefined | Find element by condition |
for loop |
custom | Complex logic needed |
Conclusion
Use includes() for simple value checking as it's the most readable and efficient method. For finding positions or complex conditions, consider indexOf(), find(), or custom loops.
