Array search from the end with lastIndexOf()
In this chapter you will learn:
Array lastIndexOf()
lastIndexOf syntax:
lastIndexOf(itemToLookFor, optionalIndexToStartWith)
lastIndexOf()
starts from the
back of the array to do the search.
lastIndexOf()
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><!--from j a va2 s.c o m-->
<html>
<head>
<script type="text/javascript">
var numbers = [1,2,3,4,5,4,3,2,1];
document.writeln(numbers.lastIndexOf(4)); //5
document.writeln(numbers.lastIndexOf(4, 4)); //3
var person = { name: "JavaScript" };
var people = [{ name: "JavaScript" }];
var morePeople = [person];
document.writeln(morePeople.indexOf(person)); //0
</script>
</head>
<body>
</body>
</html>
Next chapter...
What you will learn in the next chapter:
Home » Javascript Tutorial » Array