Easy Tutorial
❮ Met Document Importnode Jsref Regexp Nx ❯

JavaScript lastIndexOf() Method

JavaScript String Object

Example

Find the last occurrence of the string "tutorialpro":

var str = "I am from tutorialpro, welcome to tutorialpro site.";
var n = str.lastIndexOf("tutorialpro");

n Output:

28

Definition and Usage

The lastIndexOf() method returns the position of the last occurrence of a specified string value. If a second parameter start is specified, it searches backward from the specified position in the string.

Note: This method searches backward for the string but returns the position calculated from the start (0). It checks if the string contains the specified value.

The search starts at the specified position start or at the end of the string (if start is not specified).

If no matching string is found, it returns -1.

Note: The lastIndexOf() method is case-sensitive!

Tip: You can also refer to the similar method indexOf().


Browser Support

All major browsers support the lastIndexOf() method.


Syntax

Parameter Values

Parameter Description
searchvalue Required. The string value to search for.
start Optional integer parameter. Specifies the position in the string to start the search from. Its valid range is from 0 to stringObject.length - 1. If omitted, the search starts from the last character of the string.

Return Value

Type Description
Number The position of the last occurrence of the searched string, or -1 if no match is found.

Technical Details

| JavaScript Version: | 1.0 | | --- | --- |


More Examples

Example

Find the last occurrence of the string "tutorialpro" starting from the 20th character:

var str = "I am from tutorialpro, welcome to tutorialpro site.";
var n = str.lastIndexOf("tutorialpro", 20);

n Output:

10

Example

Find the last occurrence of the string "tutorialpro" starting from the 10th character backward:

var str = "I am from tutorialpro, welcome to tutorialpro site.";
var n = str.lastIndexOf("tutorialpro", 9);

n Output:

-1

JavaScript String Object

❮ Met Document Importnode Jsref Regexp Nx ❯