English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript String lastIndexOf() Method

 JavaScript String Object

lastIndexOf()The method returns the last occurrence position of the specified value in the string.

If the value cannot be found, it will return-1.

If the value exists multiple times, it will return the position of the last occurrence.

If you want to search from the beginning to the end, please useindexOf()method.

Note:For more information on Array methods, please refer toArray.lastIndexOf().

Syntax:

string.lastIndexOf(searchValue, start)
var str = 'Hello world, I repeat Hello world';
str.lastIndexOf('Hello');
Test and see‹/›

Note:This method is case-sensitive.

Browser Compatibility

All browsers fully support the lastIndexOf() method:

Method
lastIndexOf()IsIsIsIsIs

Parameter Value

ParameterDescription
searchValue(Required) A string representing the value to search for
start(Optional) An integer representing the index to start searching from (search forward); the default is the length of the string

Technical Details

Return value:the last occurrence ofsearchValueindex, if not found, then-1
JavaScript version:ECMAScript 1

More examples

Return the last position of the character 'O' in the string, at position5Start searching (search forward):

var str = 'HELLO WORLD HELLO';
str.lastIndexOf('O', 5);
Test and see‹/›

 JavaScript String Object