JS: Array.prototype.lastIndexOf

By Xah Lee. Date: . Last updated: .

myArray.lastIndexOf(value)

myArray.lastIndexOf(value, start_index)

Return the index of right-most element that is value. Search starts at the end, or at start_index, from right to left.

Return -1 if not found.

[see JS: Array.prototype.indexOf]

const rr = [2, 3, 5, 6, 3];

console.log ( rr.lastIndexOf ( 3 ) ); // 4
// search starts from right

// start search starts from index 1
console.log ( rr.lastIndexOf ( 3, 1 ) ); // 1

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

Reference

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

Array Search Topic

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

Array Topic

  1. JS: Array Basics
  2. JS: Understand JS Array
  3. JS: Create Array
  4. JS: Sparse Array
  5. JS: Array-Like Object
  6. JS: Array How-To

  1. JS: Array Object
  2. JS: Array.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth