Check If Value Exists In A Javascript Array Example

01-Jun-2020

.

Admin

Hi Guys,

In this blog, I will show how to check if value exists in a javascript array.we will explain check if value exists in an array javascript. we will show you how to check if the value exists in a javascript array there are three types of checks if a value exists in a javascript array.

Here example of check if value exists in a javascript array.

Solution 1: using indexOf() function


Now in this example,You can use indexOf() function for check the value exists in an array or no. it is function returns the true and false value. see below example of indexOf().

<script>

var myArray = ["PHP", "Laravel", "Asp.net", "Html", "Java"];

if(myArray.indexOf("Laravel") !== -1){

alert("Value exists!");

} else{

alert("Value does not exists!");

}

</script>

output

Value exists!

Solution 2: using includes() function

Here in this example,You can use includes() function for check the value exists in an array or no. it is function returns the true and false value. see below example of includes().

<script>

var myArray = ["PHP", "Laravel", "Asp.net", "Html", "Java"];

alert(myArray.includes("PHP")); // Outputs: true

</script>

output

Value exists!

Solution 3: using inArray() function

Here in this example,You can use inArray() function for check the value exists in an array or no. it is function returns the true and false value. see below example of inArray().

var myArray = ["PHP", "Laravel", "Asp.net", "Html", "Java"];

if ($.inArray('Html', myArray) >= 0) {

alert("Value exists!");

}else {

alert("Value does not exists!");

}

output

Value exists!

It will help you..

#JavaScript