15.4.4.15 Array.prototype.lastIndexOf(searchElement[, fromIndex])

2010-07-05

int Array.prototype.lastIndexOf(searchElement:mixed[, fromIndex:int|mixed=0])

Return the last index where given searchElement was found (the right most index), or -1 if the element was not found. If fromIndex was given, the search starts from that index (still returning -1 if not found). If fromIndex is negative, fromIndex+length is used as start.

This function uses strict comparison (===, 11.9.6).

The this value is coerced to an object, fromIndex (if present) to an unsigned int.

Array.prototype.indexOf.length = 1

Code: (Meta Ecma)
Array.prototype.indexOf = function(searchElement, fromIndex){
var O = ToObject(this);
var lenValue = O.[[Get]]("length");
var len = ToUint32(lenValue);
if (len == 0) return -1;
if (arguments.length >= 2) var n = ToInteger(fromIndex);
else n = len;
if (n >= 0) var k = Math.min(n, len-1);
else var k = len - Math.abs(n);
while (k >= 0) {
var kPresent = O.[[HasProperty]](ToString(k));
if (kPresent) {
var elementK = O.[[Get]](ToString(k));
var same = StrictEqualityComparison(searchElement, elementK); // 11.9.6
if (same) return k;
}
--k;
}
return -1;
}