Signup/Sign In
LAST UPDATED: AUGUST 4, 2021

How to check if a value exists in an array in JavaScript?

Answer: Using indexOf() method

There are several ways to find out whether the value exists in an array in JavaScript or not. The methods that we are going to cover in this lesson are given below:

  • Using indexOf() method
  • Using includes() method
  • Using loops

Using indexOf() method

The indexOf() method is used to find the position of the element within the array. The search will start at the index 0 if the element's position is not specified, and the search will end at the end of the array.

This method returns the element's position if it is present within the array and returns -1 if the element is not present within the array.

This method returns the first occurrence of the element if it is present more than once within the array.

Syntax

array.indexOf(item, start)

Example: Using indexOf() method

In this example, we have checked whether the value exists within the array or not using indexOf() method.

<!DOCTYPE html>
<html lang="en">
<head>
	<title>check if a value exists in an array</title>
</head>
<body>
	<script>    
     var progLang = ["C", "Java", "C++", "Python", "PHP"];

     if(progLang.indexOf("Java") !== -1){
    	console.log("Element exists");
     }else{
    	console.log("Element does not exist");
     }
    </script>
</body>
</html>

Output


Element exists

Using includes() method

Apart from the indexOf() method, we can also check whether the element exists within an array or not using the includes() method.

The includes() method determines whether an array consists of the specified element or not. This method returns a boolean value.

It returns true if the element exists within the array else it returns false.

Syntax

includes(element)
includes(element, index)

Example: Using includes() method

In this example, we have checked whether the value within the array exists or not using the includes() method. The output will be true if the value exists otherwise it returns false.

<!DOCTYPE html>
<html lang="en">
<head>
	<title>check if a value exists in an array</title>
</head>
<body>
	<script>    
    var progLang = ["C", "Java", "C++", "Python", "PHP"];

    console.log(progLang.includes("C"));
    console.log(progLang.includes("Ruby"));

</script>
</body>
</html>

Output


true
false

Using loops

We can also check whether the value exists within the array or not using loops.

Example: Using for loop

In this example, we have to check whether the value exists within the array or not by traversing the entire array using for loop.

<!DOCTYPE html>
<html lang="en">
<head>
	<title>check if a value exists in an array</title>
</head>
<body>
	<script type = 'text/javascript' >
		var progLang = ["C", "JavaScript", "Python", "C++", "Java"];
		function checkValue(value, arr) {
		    var status = 'Value does not exist';
		    for (var i = 0; i < arr.length; i++) {
		        var name = arr[i];
		        if (name == value) {
		            status = 'Value Exist';
		            break;
		        }
		    }
		    return status;
		}
		console.log(checkValue('C', progLang));
		console.log(checkValue('PHP', progLang)); 
	</script>
</body>
</html>

Output

Value Exist
Value does not exist

Conclusion

Here, we have discussed how to check whether a value exists within the JavaScript array or not. There are various ways to do this, but we have discussed three ways to determine whether the value exists within the array or not. In the first method, we have used the indexOf() method. This method returns the index of the value if present otherwise, it returns false. Then we used the method. This method returns true if the value exists within the array otherwise it returns false. And in the third method, we have used to for loop.



About the author: