JS: String.prototype.lastIndexOf

By Xah Lee. Date: . Last updated: .

str.lastIndexOf(str)

str.lastIndexOf(str, start_search_pos)

Return the index where the first occurrence of first char of str, starting search at start_search_pos, from right to left. Returns -1 if not found.

start_search_pos by default is 0.

const ss = "abcabc";

console.log ( ss.lastIndexOf ( "b" ) ); // 4

// start at index 1 (including index 1)
console.log ( ss.lastIndexOf ( "b", 1 ) ); // 1

// more than 1 char
console.log ( ss.lastIndexOf ( "bc", 2 ) ); // 1

// even if the substring pass start index, it's still considered found
console.log ( ss.lastIndexOf ( "bca", 1 ) ); // 1

// example of not found, returns -1
console.log ( ss.lastIndexOf ( "xy" ) ); // -1

JS String

  1. String Overview
  2. Template String
  3. Char, Code Unit, Codepoint
  4. String Escape Sequence
  5. Unicode Escape Sequence

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Object Ref
  5. DOM Scripting
  6. SVG
  7. Blog