String.prototype.lastIndexOf()
lastIndexOf(searchString, fromIndex?) Returns:
number · Added in ves6 · Updated March 13, 2026 · String Methods javascript es6 string search
The lastIndexOf() method searches for the last occurrence of a substring within a string, starting from the end. It returns the index position where the substring starts, or -1 if the substring is not found. This method is the inverse of indexOf(), which searches from the beginning.
Syntax
string.lastIndexOf(searchString)
string.lastIndexOf(searchString, fromIndex)
searchString: The substring to search for. If omitted,lastIndexOf()searches for the string"undefined".fromIndex(optional): The index to start searching backward from. Defaults tostring.length - 1. If greater than or equal to the string length, the entire string is searched.- Returns: A number representing the index of the last occurrence, or
-1if not found.
TypeScript Signature
interface String {
lastIndexOf(searchString: string, fromIndex?: number): number;
}
Basic Examples
const text = "hello world hello";
console.log(text.lastIndexOf("hello")); // 12 (last occurrence)
console.log(text.lastIndexOf("world")); // 6
console.log(text.lastIndexOf("!")); // -1 (not found)
Using the fromIndex Parameter
The second parameter controls where the search starts from the end:
const text = "hello world hello";
console.log(text.lastIndexOf("hello")); // 12 (searches entire string)
console.log(text.lastIndexOf("hello", 10)); // 0 (stops before index 10)
// Finding all occurrences manually
const str = "to be or not to be";
const firstBe = str.lastIndexOf("be"); // 16
const secondBe = str.lastIndexOf("be", 15); // 3
console.log(firstBe, secondBe); // 16, 3
Practical Use Case: File Extension Extraction
lastIndexOf() is ideal for extracting file extensions from paths:
function getExtension(filename) {
const dotIndex = filename.lastIndexOf(".");
if (dotIndex === -1 || dotIndex === 0) {
return ""; // No extension or hidden file
}
return filename.slice(dotIndex + 1);
}
console.log(getExtension("document.pdf")); // "pdf"
console.log(getExtension("image.png")); // "png"
console.log(getExtension("archive.tar.gz")); // "gz" (last dot)
console.log(getExtension("README")); // "" (no extension)
Finding the Last Separator in a Path
function getBasename(filepath) {
const lastSlash = Math.max(
filepath.lastIndexOf("/"),
filepath.lastIndexOf("\\")
);
return lastSlash === -1 ? filepath : filepath.slice(lastSlash + 1);
}
console.log(getBasename("/home/user/documents/file.txt")); // "file.txt"
console.log(getBasename("C:\\Users\\Admin\\data.csv")); // "data.csv"
Case Sensitivity
Like indexOf(), lastIndexOf() is case sensitive:
const text = "JavaScript JavaScript";
console.log(text.lastIndexOf("Java")); // 10
console.log(text.lastIndexOf("java")); // -1 (lowercase doesn't match)
console.log(text.lastIndexOf("SCRIPT")); // -1 (uppercase doesn't match)
Edge Cases
const text = "hello";
// Empty string returns fromIndex or string length
console.log(text.lastIndexOf("")); // 5 (string length)
console.log(text.lastIndexOf("", 3)); // 3
console.log(text.lastIndexOf("", -1)); // 5 (treated as 0)
// fromIndex greater than string length
console.log(text.lastIndexOf("h", 100)); // 0 (searches entire string)
// Negative fromIndex
console.log(text.lastIndexOf("h", -5)); // -1 (searches nothing)
// Search string longer than target
console.log(text.lastIndexOf("hello world")); // -1
Key Differences from indexOf()
| Aspect | indexOf() | lastIndexOf() |
|---|---|---|
| Search direction | Forward (start → end) | Backward (end → start) |
| Default fromIndex | 0 | string.length - 1 |
| Use case | First occurrence | Last occurrence |
Browser Compatibility
| Browser | Version |
|---|---|
| Chrome | 1+ |
| Firefox | 1+ |
| Safari | 1+ |
| Edge | 12+ |
| Node.js | 0.1.0+ |
Universal support across all modern and legacy JavaScript environments.
See Also
- string::slice — Extract a portion of a string
- string::split — Split a string into an array
- string::includes — Check if a string contains a substring