Check If Value Exists In A Javascript Array Example
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..
- How to set Date in setUTCHours() Method in JavaScript?
- How to Date sets UTC milliseconds in Javascript?
- How to Use push(), unshift(),pop() and shift() With Array in Javascript?
- How to Check if a Variable is a Number in Javascript?
- How To Format a Number Using Fixed - Point Notation in Javascript?
- How to Insert an Element into an Array in javaScript?
- How to remove duplicate elements from JavaScript Array?
- How to calculate years, months and days between two dates in javascript?