JavaScript: Array.prototype.lastIndexOf

By Xah Lee. Date: . Last updated: .

myArray.lastIndexOf(…)

Same as indexOf(…) but search start from end of list, right-to-left.

〔►see JavaScript: Array.prototype.indexOf

var rr = [3,4,5,3];

console.log ( rr.lastIndexOf ( 3 ) ); // 3
console.log ( rr.indexOf ( 3 ) ); // 0
var rr = [4, 3, 5, 6, 3];

console.log ( rr.lastIndexOf ( 3, 0 ) ); // -1
// not found.

console.log ( rr.lastIndexOf ( 3, 1 ) ); // 1

Reference

ECMAScript® 2016 Language Specification#sec-array.prototype.lastindexof

Array Search Topic

  1. JavaScript: Array.prototype.indexOf
  2. JavaScript: Array.prototype.lastIndexOf
  3. JavaScript: Array.prototype.find
  4. JavaScript: Array.prototype.findIndex
  5. JavaScript: Array.prototype.includes

Array Topic

  1. JavaScript: Array Basics
  2. JavaScript: Understand JS Array
  3. JavaScript: Create Array
  4. JavaScript: Sparse Array
  5. JavaScript: What's Array-Like Object?
  6. JavaScript: How to Convert Array-Like Object to Array?
  7. JavaScript: Array How-To

  1. JavaScript: Array Object
  2. JavaScript: Array.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.