JavaScriptStrings

String.prototype.lastIndexOf()

Returns the index of the last occurrence of the given text in a string, or -1 when it is not there.

Returns
The index where the last occurrence starts, or -1 when the text was not found.
Changes the original
No
On this page

Example

#
JavaScript
const file = 'project.final.v2.html'

console.log(file.lastIndexOf('.'))
console.log(file.indexOf('.'))
console.log(file.slice(file.lastIndexOf('.') + 1))
console.log(file.lastIndexOf('.css'))
Console
16
7
html
-1

Definition and usage

#

The lastIndexOf() method searches a string from the end and returns the position where the last occurrence of the search text starts. The position is still counted from the start of the string, from zero. When the text is not found, the result is -1.

It is most often used to pull out the tail end of text after the last separator: a file extension after the last period, a file name after the last slash in a path, or the last segment of a URL. Paired with slice(), a single line is all it takes.

The second argument limits the search: the method only considers occurrences that start at that position or earlier. Letter case matters, and regular expressions are the tool for searching by pattern instead of fixed text.

Syntax

#
Syntax
string.lastIndexOf(searchString)
string.lastIndexOf(searchString, position)

Parameters

#
  • searchString

    string

    The text to search for. Letter case matters.
  • position

    number, optional

    The highest index at which a match can start. By default, the whole string is searched.

More examples

#
File name and folder from a path
JavaScript
const path = 'courses/javascript/lessons/loops.md'
const fileName = path.slice(path.lastIndexOf('/') + 1)
const folder = path.slice(0, path.lastIndexOf('/'))

console.log(fileName)
console.log(folder)
Console
loops.md
courses/javascript/lessons
Searching before a given position
JavaScript
const text = 'ok, error, ok, error, ok'

const last = text.lastIndexOf('error')
const previous = text.lastIndexOf('error', last - 1)

console.log(last, previous)
Console
15 4
Trap: no separator
JavaScript
function getExtension(fileName) {
  const dot = fileName.lastIndexOf('.')
  return dot === -1 ? '(none)' : fileName.slice(dot + 1)
}

console.log('README'.slice('README'.lastIndexOf('.') + 1))
console.log(getExtension('README'))
console.log(getExtension('styles.min.css'))
Console
README
(none)
css

Without a dot, lastIndexOf() returns -1, and slice(-1 + 1) is slice(0), which is the whole string. So before cutting anything out, check whether the separator even exists.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Always check the result against -1. The expression slice(index + 1) returns the whole string when there is no separator, which is easy to miss.
  • endsWith() is more readable for checking just the end of a string, such as a .pdf extension.

Related entries

#

Spotted a mistake or missing an example? Write to us.