Favorites

JavaScript String.prototype.lastIndexOf() Method

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

String.prototype.lastIndexOf()
JavaScript String Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

The lastIndexOf() method returns the position of the last occurrence of some text in a string, or -1.

Description

The lastIndexOf() method returns the index of the last occurrence of the search text, searching backwards, or -1 if it isn't found. A common use is finding the last dot in a file name.

The optional position argument limits the search to matches that start at or before that index, which lets you step backwards through every occurrence in a loop. An empty search string returns the position, or the string's length.

It's useful for working with paths and file names: everything after the last slash is the file name, and everything after the last dot is the extension. For full URLs and paths, the URL object or a library may handle edge cases, such as query strings and dots in folder names, more reliably.

Syntax

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

Parameters

  • searchStringThe text to search for.
  • positionOptional. The index to search backwards from.

Return Value

The index of the last match, or -1 if it isn't found.

Example

const path = 'archive.tar.gz';
const dot = path.lastIndexOf('.');
console.log(dot, path.slice(dot + 1));

Browser Support

The following information will show you the current browser support for the String.prototype.lastIndexOf() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.