When writing JavaScript, I am usually using the jQuery library. I also seem to work with arrays a lot. Most of the time I only care if an item is an existing array or not. That's pretty simple to do with jQuery, and looks like this:
// First, the array.
var mySimpleArray = ["apple", "juice"];
if (!$.inArray("apple", mySimpleArray) == -1) {
// Item is in the array.
console.log("Apple was in the array.");
} else {
// Item is not in the array.
console.log("Apple was not in the array.");
}
The takeaway here is that when we use the $.inArray
function, we check it's value against -1
. -1
indicates that the item was not in the array.
{{ name }}