I want to be able to see if "megan" is a part of the array people. Though, i can only do this by using people[2].
Here is my code:
var people = new Array("jack","ian");
document.write(people[0]);
people.push("megan");
document.write("<br />");
I want to be able to see if "megan" is a part of the array people. Though, i can only do this by using people[2].
Here is my code:
var people = new Array("jack","ian");
document.write(people[0]);
people.push("megan");
document.write("<br />");
Use the indexOf
method of the array:
if(people.indexOf("megan") > -1) {
//do stuff
} else {
//not in array
}
If the string is in the array, 0
is returned. if not, -1
is returned.