JavaScript - Arrays - lastIndexOf


// JavaScript - Array.prototype.lastIndexOf()
The lastIndexOf() method returns the last index at which a given element can be 
found in the array, or -1 if it is not present. The array is searched backwards, 
starting at fromIndex.

arr.lastIndexOf(searchElement, fromIndex)

searchElement: Element to locate in the array.
fromIndex: Optional.  The index at which to start searching backwards. Defaults 
  to the array's length minus one (arr.length - 1), i.e. the whole array will 
  be searched. If the index is greater than or equal to the length of the array, 
  the whole array will be searched. If negative, it is taken as the offset from 
  the end of the array. Note that even when the index is negative, the array is 
  still searched from back to front. If the calculated index is less than 0, -1 
  is returned, i.e. the array will not be searched.

The lastIndexOf method returns the last index of the element in the array or 
-1 if not found.

The lastIndexOf method compares searchElement to elements of the Array using 
strict equality (the same method used by the ===, or triple-equals, operator).

var array = [2, 5, 9, 2];
array.lastIndexOf(2);     // 3
array.lastIndexOf(7);     // -1
array.lastIndexOf(2, 3);  // 3
array.lastIndexOf(2, 2);  // 0
array.lastIndexOf(2, -2); // 0
array.lastIndexOf(2, -1); // 3
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License