Array search from start with indexOf()
In this chapter you will learn:
Array indexOf()
Its syntax:
indexOf(itemToLookFor, optionalIndexToStartWith)
indexOf()
starts searching from the
front of the array.
indexOf()
returns the position of the item
in the array or -1 if the item isn't in the array.
An identity comparison is used during comparing.
<!DOCTYPE html><!-- j a va2s . c o m-->
<html>
<head>
<script type="text/javascript">
var numbers = [1,2,3,4,5,4,3,2,1];
document.writeln(numbers.indexOf(4)); //3
document.writeln(numbers.indexOf(4, 4)); //5
var person = { name: "JavaScript" };
var people = [{ name: "JavaScript" }];
var morePeople = [person];
document.writeln(people.indexOf(person)); //-1
</script>
</head>
<body>
</body>
</html>
Next chapter...
What you will learn in the next chapter:
Home » Javascript Tutorial » Array