Using String lastIndexOf() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the string method called lastIndexOf() with syntax and examples.

Description

In JavaScript, lastIndexOf() is a string method that is used to discover the region of a substring in a string, looking out the string backwards. Because the lastIndexOf() approach is a method of the String object, it ought to be invoked via a unique occasion of the String class.

Syntax

In JavaScript, the syntax for the lastIndexOf() method is:

string.lastIndexOf(substring [, start_position]);

Parameters or Arguments

substring It is the substring that you want to locate inside string. start_position Optional. It is the role in string where the search will start. The first position in string is zero and the closing role in string is string.length-1. If this parameter is now not provided, the search for substring starts offevolved at the stop of string and the full string is searched.

Returns

The lastIndexOf() method returns the function of the first prevalence of substring in string when looking out the string backwards. The first role in the string is zero

If the lastIndexOf() approach does no longer find the substring in string, it will return -1.

Note

The lastIndexOf() technique performs a case-sensitive search. Even even though the lastIndexOf() method searches the string backwards, it nonetheless returns a vicinity price that is relative to the begin of the string. For example, a return price of zero is the area of the first persona in the string, a return price of 1 is the vicinity of the second personality in the string, and so on. The lastIndexOf() technique does now not change the fee of the authentic string.

Example

Let’s take a seem at an instance of how to use the lastIndexOf() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.lastIndexOf('T'));

In this example, we have declared a variable called totn_string that is assigned the string fee of ‘TechOnTheNet’. We have then invoked the lastIndexOf() approach of the totn_string variable to look for a substring inside totn_string searching backwards.

We have written the output of the lastIndexOf() approach to the web browser console log, for demonstration purposes, to show what the lastIndexOf() technique returns.

The following will be output to the web browser console log:

6

In this example, the lastIndexOf() technique back 6. Because the string is search backwards, it found the first occurrence of T’ inside ‘TechOnTheNet’ in role 6 of the string.

Specifying a Start Position Parameter

You can trade the position where the search will begin in the string by way of offering a start_position parameter to the lastIndexOf() method.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.lastIndexOf('T',4));

The following will be output to the net browser console log:

0

In this example, we have set the start_position parameter to a fee of four This skill that the search will begin looking for the value ‘T’ beginning at role 4 in the string and search the string backwards from there. So in this case, the substring ‘T’ is found at role 0 in the string ‘TechOnTheNet’.

Specifying Multiple Characters as the Substring

Next, the lastIndexOf() approach can search for multiple characters in a string.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.lastIndexOf('ch'));

The following will be output to the web browser console log:

2

In this example, the lastIndexOf() approach back 2 which is the function of ‘ch’ in the string ‘TechOnTheNet’ when looking the string in reverse (ie: from quit to establishing of string).

Since the lastIndexOf() approach can only return one value, it will return the function of the substring’s first character when the prevalence is found, even although the substring is more than one characters in length.

No Occurrence is Found

Finally, the lastIndexOf() method will return -1 if an occurrence of substring is no longer found in string.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.lastIndexOf('z'));

The following will be output to the web browser console log:

-1

In this example, the lastIndexOf() technique lower back -1 because the substring ‘z’ is no longer discovered in the string ‘TechOnTheNet’.