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
-1when the text was not found. - Changes the original
- No
Example
#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'))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
#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
#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)const text = 'ok, error, ok, error, ok'
const last = text.lastIndexOf('error')
const previous = text.lastIndexOf('error', last - 1)
console.log(last, previous)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'))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 expressionslice(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.pdfextension.
Related entries
#- String.prototype.indexOf()Returns the index of the first occurrence of the given text in a string, or -1 when it is not there.
- String.prototype.slice()Returns a portion of a string from a start index to an end index (not included). Supports negative indexes.
- String.prototype.endsWith()Checks whether a string ends with the given text. Returns true or false.
- String.prototype.includes()Checks whether a string contains the given piece of text. Returns true or false, and is case-sensitive.
- Array.prototype.lastIndexOf()Returns the index of the last occurrence of a value in the array, or -1 when it isn’t there.
Spotted a mistake or missing an example? Write to us.