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>
<html>
<head>
<title>Example</title>
<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>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Array:
- The Array Type
- Array Built-in Methods
- Detecting Arrays
- Get and set array values
- Enumerating the Contents of an Array
- Array Length
- Array join() method
- Array concat()
- Array indexOf()
- Array lastIndexOf()
- Array every()
- Array filter() filters array with the given function.
- Array map()
- Array forEach()
- push() and pop():Array Stack Methods
- push(), shift():Array Queue Methods
- Array reduce()
- Array reduceRight()
- reverse():Reordering array
- Array slice()
- Array some()
- Array splice()
- Array sort()
- toString(), toLocaleString() and valueOf Array
- Array unshift()