javascript
Write a function to check if milk exists in your array
var items = ["milk", "bread", "sugar"];
function checkForProduct(item) {
if (items.indexOf(item) === -1) {
console.log("item does not exist");
} else {
console.log("item is in your list");
}
}
checkForProduct("socks"); //item does not exist
checkForProduct("milk"); //item is in your list
Output
checkForProduct("socks"); //item does not exist
checkForProduct("milk"); //item is in your list
checkForProduct("milk"); //item is in your list
Was this helpful?
Similar Posts
- How to Check If a Value Exists in an Array
- Add service worker to your website
- Install redux form in your app
- Pass a function as parameter of another function in Javascript
- Check if all array items are equal javascript
- Check if given value is an array in Javascript
- How to Check If an Array Includes an Object