javascript
How to Check If a Value Exists in an Array
<script>
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
alert(fruits.includes("Banana")); // Outputs: true
alert(fruits.includes("Coconut")); // Outputs: false
alert(fruits.includes("Orange")); // Outputs: true
alert(fruits.includes("Cherry")); // Outputs: false
</script>
<script>
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
// Check if a value exists in the fruits array
if(fruits.indexOf("Mango") !== -1){
alert("Value exists!")
} else{
alert("Value does not exists!")
}
</script>
Was this helpful?
Similar Posts
- Write a function to check if milk exists in your array
- Convert object {key: value} to Array [[key, value]] Javascript
- Check if given value is an array in Javascript
- Finding the max/min value of an attribute in an array of objects
- Find the minimum value from an array javascript
- Find the maximum value from an array using Javascript
- Replace array values with another value Lodash