JS Built-ins
StringString.prototype.lastIndexOfbeginner

String.prototype.lastIndexOf() β€” multiple occurrences

πŸ“– Usage Example
const str = 'abcabc'
str.lastIndexOf('a')    // β†’ 3
str.lastIndexOf('z')    // β†’ -1
str.lastIndexOf('b', 3) // β†’ 1

Use lastIndexOf() to find the index of the last occurrence of a substring, searching from right to left.

String.prototype.lastIndexOf() β€” Multiple Occurrences

When there are multiple matches, .lastIndexOf() returns the rightmost one.

Challenge: Implement lastOccurrence(str, ch) using .lastIndexOf().

lastOccurrence('abracadabra', 'a') // β†’ 10
πŸ’‘ Hints (1)
  • β€’ `.lastIndexOf()` scans from the end, so it finds the rightmost match.