How to check whether an array includes a particular value or not in JavaScript ?
Last Updated :
12 Jan, 2022
In this article, we will discuss the construction of an array followed by checking whether any particular value which is required by the user is included (or present) in the array or not.
But let us first see how we could create an array in JavaScript using the following syntax-
Syntax: The following syntax would be helpful for any user for the creation of an array with ease-
let array = [item_1 , item_2, item_3 , ...];
Now we have seen the creation of an array let us see several approaches to check whether an array includes any value in itself which a user wishes to have a look over it or not.
Following are certain approaches that we may implement to check whether an array includes a value or not-
Approach 1: This is initial, traditional as the most common approach which any individual could think of in beginning. In this approach, we will run a for loop but before running the for loop, we will initialize our array and the value which we are looking for. Inside that for loop, we will see if our value is present in our array then along with the value name, we will return the index of that variable. If the value is not present in our array then we will exit from the for loop and then will print a message that the value is not present in the array.
Example:
HTML
<script>
let fruits_array = [
"mango",
"banana",
"apple",
"pineapple",
"pomegranate",
"orange",
];
let valueChecker = (value) => {
for (let i = 0; i < fruits_array.length; i++) {
let current_value = fruits_array[i];
if (value === current_value) {
return value + " is present at index: " + i;
}
}
return value + " is not included in this array..";
};
console.log(valueChecker("apple"));
console.log(valueChecker("app"));
console.log(valueChecker("banana"));
</script>
Output:
apple is present at index: 2
app is not included in this array..
banana is present at index: 1
Approach 2: After analyzing the above traditional most commonly used approach, now this is the newest approach. In this approach, we will be using .includes() method to check the value present in the array or not. If the value is present then we will print the message illustrating that value is present in an array. If the value is not present then we will print the message illustrating that value is not present.
Example:
HTML
<script>
let fruits_array = [
"mango",
"banana",
"apple",
"pineapple",
"pomegranate",
"orange",
];
let value_1 = "apple";
let value_2 = "app";
console.log(fruits_array.includes(value_1));
console.log(fruits_array.includes(value_2));
</script>
Output:
true
false
Approach 3: In this approach, we will be using indexOf() method. By using this method, we will be checking if the index value of the particular element which we are looking for, is greater than or equal to zero then we will print the message illustrating that the element is present at a certain index value. If our element is not present in the array, then we will be displaying an error message illustrating that the element is missing from the array.
HTML
<script>
let fruits_array = [
"mango",
"banana",
"apple",
"pineapple",
"pomegranate",
"orange",
];
let checkValue = (value) => {
if (fruits_array.indexOf(value) >= 0)
return value + " is present at index : "
+ fruits_array.indexOf(value);
else
return value + " is not present in this array";
};
console.log(checkValue("apple"));
console.log(checkValue("app"));
console.log(checkValue("mango"));
</script>
Output:
apple is present at index : 2
app is not present in this array
mango is present at index : 0
Similar Reads
How to check if an array includes an object in JavaScript ?
Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array.How to check if an array inclu
3 min read
How to Check if a Variable is an Array in JavaScript?
To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
2 min read
How to check if the value is primitive or not in JavaScript ?
To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
3 min read
JavaScript - Check if JS Array Includes a Value?
To check if an array includes a value we can use JavaScript Array.includes() method. This method returns a boolean value, if the element exists it returns true else it returns false.1. Using Array.includes() Method - Mostly Used The JS array.includes() method returns true if the array contains the s
4 min read
How to check whether an array is subset of another array using JavaScript ?
The task is to check whether an array is a subset of another array with the help of JavaScript. There are a few approaches, we will discuss below: Approaches:using JavaScript array.every() methodusing JavaScript array.some() methodApproach 1: Using JavaScript array.every() methodThis approach checks
2 min read
How to Check Object is an Array in JavaScript?
There are two different approaches to check an object is an array or not in JavaScript.1. Using Array.isArray() MethodThe Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false.
1 min read
How to check if a Variable Is Not Null in JavaScript ?
In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
4 min read
How to check an Array Contains an Object of Attribute with Given Value in JavaScript ?
Sometimes, when working with an array of objects in JavaScript, we need to determine whether an object with a specific attribute value exists in the array. This can be useful when filtering or searching for specific objects in the array. Below are the common methods to determine if the array contain
4 min read
How to Check if an Array Includes an Object in TypeScript ?
In TypeScript, checking if an array includes an object consists of comparing the object's properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method. There are several ways to check if an array includes an object
3 min read
How to Check a Value Exist at Certain Array Index in JavaScript ?
Determining if a value exists at a specific index in an array is a common task in JavaScript. This article explores various methods to achieve this, providing clear examples and explanations to enhance your coding efficiency.Below are the different ways to check if a value exists at a specific index
5 min read