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:

1.
lastIndexOf(searchString)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe 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.

2.
lastIndexOf(searchString, fromIndex)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe string to search for within this string.
fromIndexoptionalThe 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,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the lastIndexOf() method with the argument 'o' to find the last occurrence of the character 'o'.
  3. The result is stored in the variable index.
  4. We log index to the console using the console.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,

  1. We define a string variable str with the value 'Hello, world! Hello!'.
  2. We use the lastIndexOf() method with the arguments 'Hello' and 15 to find the last occurrence of the substring 'Hello', starting the search backward from position 15.
  3. The result is stored in the variable index.
  4. We log index to the console using the console.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,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the lastIndexOf() method with the argument 'JavaScript' to search for the substring 'JavaScript' within the string.
  3. The result is stored in the variable index.
  4. We log index to the console using the console.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.