December 2013
Intermediate to advanced
384 pages
9h 54m
English
var food = ["broccoli", "carrots", "spinach"]; var a = food.indexOf("spinach"); // a = 2 var b = food.indexOf("pizza"); // b = -1
The simplest way to check whether an item already exists in an array is to use the .indexOf(item). If the item is found, the index of the first instance is returned; otherwise, –1 is returned.
Read now
Unlock full access