JavaScript String lastIndexOf()
Syntax & Examples
String.lastIndexOf() method
The lastIndexOf() method of the String class in JavaScript returns the index within the calling String object of the last occurrence of the specified value, searchString, starting the search at fromIndex. Returns -1 if the value is not found.
Syntax of String.lastIndexOf()
There are 2 variations for the syntax of String.lastIndexOf() method. They are:
lastIndexOf(searchString)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchString | required | The string to search for within this string. |
This method returns the index within the calling String object of the last occurrence of searchString.
Returns value of type Number
.
lastIndexOf(searchString, fromIndex)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchString | required | The string to search for within this string. |
fromIndex | optional | The position in the calling string to start the search backward from. Defaults to the length of the string. |
This method returns the index within the calling String object of the last occurrence of searchString, starting the search backward at fromIndex.
Returns value of type Number
.
✐ Examples
1 Using lastIndexOf() method to find the last occurrence of a substring
In JavaScript, we can use the lastIndexOf()
method to find the last occurrence of a specified substring.
For example,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
lastIndexOf()
method with the argument'o'
to find the last occurrence of the character'o'
. - The result is stored in the variable
index
. - We log
index
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello, world!';
const index = str.lastIndexOf('o');
console.log(index);
Output
8
2 Using lastIndexOf() method with a specified start position
In JavaScript, we can use the lastIndexOf()
method to find the last occurrence of a specified substring, starting the search backward from a given position.
For example,
- We define a string variable
str
with the value'Hello, world! Hello!'
. - We use the
lastIndexOf()
method with the arguments'Hello'
and15
to find the last occurrence of the substring'Hello'
, starting the search backward from position15
. - The result is stored in the variable
index
. - We log
index
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello, world! Hello!';
const index = str.lastIndexOf('Hello', 15);
console.log(index);
Output
0
3 Using lastIndexOf() method to search for a non-existent substring
In JavaScript, we can use the lastIndexOf()
method to search for a substring that does not exist in the string.
For example,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
lastIndexOf()
method with the argument'JavaScript'
to search for the substring'JavaScript'
within the string. - The result is stored in the variable
index
. - We log
index
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello, world!';
const index = str.lastIndexOf('JavaScript');
console.log(index);
Output
-1
Summary
In this JavaScript tutorial, we learned about lastIndexOf() method of String: the syntax and few working examples with output and detailed explanation for each example.