Check if value exists in array JavaScript ES6

While writing code it is often seen that when we have to find out whether a value array may exist or not. In Javascript, there are several ways to check whether a particular value exists in the array.

In this tutorial, I show you all the possible ways to check if a value exists in the array of objects.

Check if value exists in array JavaScript ES6

1.Check Value exists in array using Indexof() method –

We can use indexof() method of javascript to check whether a particular value or item exists in the array. The indexof() method returns the index of element if it is found in array otherwise it returns -1 if it’s is not found.

The best thing about the indexof() method is it works on all the browsers.

Example 1:

Let see the below code example using indexof() method to check if the value exists in the array.

<script> 
var empName = ["Bruce","Tom","Mike","Barb"];
// Check if a value exists in the empName array
if(empName.indexOf("Mike") != -1){ // it return 2 because Mike exist at index 2
  console.log("Employee found!");
} else{
  console.log("Employee not found!")
}
</script>

Note: The indexof() method is case sensitive means capital “M” in Mike treats small “m” differently. So it is advised to always use the toLowerCase() method while comparing.

2. Check Value Exist in Array using Includes() method

Now in modern programming, ES6 provides includes() method that is used to check the value exist in the array. It returns true if the value exists in the array else returns false.

Example 2:

<script> 
var empName = ["Bruce","Tom","Mike","Barb"];
console.log(empName.includes("Mike")); //true
console.log(empName.includes("John")); //false
</script>

By default, the includes() method searches from all elements of an array but optionally you can pass the second parameter as a starting index to start the search for different positions.

Example 3:

<script> 
var empName = ["Bruce","Tom","Mike","Barb"]; 
console.log(empName.includes("Mike", 3)); //false
</script>

In the above example, I started the search from the 3rd Index of an empName array and it returns false because “Mike” exists at Index 2.

Note: if the starting index is less than Zero, all elements of an array will be searched.

inlcudes() method is case sensitive preferably used for modern application and it only works with modern browsers if you plan to test your code in IE as well go for indexof() method.

3.Check value exists in array using Javascript Array filter 

There is one more alternative way to check whether a particular value exists in an array of objects using the javascript array filter. it returns another array of objects if the value exists and you can use the length() method to check the value exists or not. See below code example –

Example 4:

<script> 
const items =[
{ name : "PS4", price: 200},
{ name : "iPhone" , price: 450},
{ name : "iPod" , price : 80},
{ name : "Watch" , price : 150},
{ name : "MacBook", price : 900 },
{ name : "TV" , price : 600},
{ name : "Keyboard", price : 50 },
{ name : "iPad" , price : 300}
]

const filterItems = items.filter((item) => {
return item.name == "Watch";
})
if(filterItems > 0){
console.log("Item exist in array");
}
else{
console.log("Item not exist in array");
}
</script>

Here I have used the javascript arrow function same can be achieved without the arrow function as well. The filter method is popular when we have a large array of objects and we need to fetch the matching element too.

Conclusion

This is how you can check the value exist in an array of objects or not. I show you the inbuilt method as well as the alternative approach to check if the value exists in the array. I hope now you can implement the same in your code to check the array element.