How to use JavaScript string lastIndexOf method

How to use JavaScript string lastIndexOf method:

The lastIndexOf method of JavaScript string can be used to search the position of a specific character or a sub-string in a string. It returns the last occurrence of the parameter, i.e. the character or substring we want to search in the string.

Optionally it also takes a second parameter. This is of type number. It returns the last occurrence at an index smaller than or equal to this number.

In this post, we will learn the definition of this method and how to use it with examples.

Definition of lastIndexOf:

The lastIndexOf method is defined as like below:

lastIndexOf(s);
lastIndexOf(s, i);

Here,

  • s is the substring we are searching in the string. If we don’t pass this value, it will use undefined for the search string. We will learn how it behaves with examples below.
  • i is an optional value. If we pass any value to it, lastIndexOf method will return the index of the last occurrence of the parameter substring less than or equal to this value. If its value is bigger than the size of the string, the search will run till the end of the string. If it is negative, it will consider it as 0.

Return value of lastIndexOf:

The lastIndexOf method returns the index of the last occurrence of the substring it it is found in the given string. It returns -1 if it is not found.

Case-sensitivity of lastIndexOf:

The lastIndexOf() method is case sensitive.

Example 1: Example of lastIndexOf with characters:

Let’s take a look at the below program:

const givenStr = 'Hello';

console.log(givenStr.lastIndexOf('e'));
console.log(givenStr.lastIndexOf('l'));
console.log(givenStr.lastIndexOf('o'));
console.log(givenStr.lastIndexOf('@'));

Here, we are using lastIndexOf to find the last index of ‘e’, ‘l’, ‘o’ and ’@’ in the string givenStr.

It will print the below output:

1
3
4
-1
  • For ‘e’, it prints 1 which is the index of ‘e’ in ‘Hello’. The index starts from 0 in a JavaScript string. So, the index of the first character is 0, second character is 1 etc.
  • For ‘e’, it prints the index of the second ‘l’, i.e. 3.
  • For ‘o’, it prints 4.
  • ’@’ is not in the string. So, it prints -1.

Example 2: Example of lastIndexOf with sub-strings:

Let’s try with substrings. It works in a similar way:

const givenStr = 'Hello World Hello World';

console.log(givenStr.lastIndexOf('Hello'));
console.log(givenStr.lastIndexOf('World'));
console.log(givenStr.lastIndexOf('rld'));
console.log(givenStr.lastIndexOf('rldH'));

It will print:

12
18
20
-1

It returns -1 only for rldH which is not in the string givenStr.

Example 3: Example of lastIndexOf with sub-strings and index:

Let’s take a look at the below program:

const givenStr = 'Hello World Hello World';

console.log(givenStr.lastIndexOf('World'));
console.log(givenStr.lastIndexOf('World', 10));
console.log(givenStr.lastIndexOf('World', 100));
console.log(givenStr.lastIndexOf('World', -1));

It will print:

18
6
18
-1
  • For the first one, it returns the index of the second ‘World’.
  • For the second one, it returns the index of the first ‘World’, because we are passing 10 as the second parameter. The index of the second ‘World’ is 18.
  • For the third one, it returns the index of the first ‘World’.
  • For the fourth one, it returns -1 because ‘World’ is not found at index 0.

Example 4: lastIndexOf without any parameter:

If we don’t pass the substring to lastIndexOf method, it will consider this as undefined.

const givenStr = 'Hello World Hello World';

console.log(givenStr.lastIndexOf());
console.log('undefined'.lastIndexOf());

It will print:

-1
0

For the first one, it returns -1 because undefined is not found in givenStr. For the second one, it prints 0 because undefined is found in the index 0 of ‘undefined’.

You might also like: