Latest

recent

JavaScript lastIndexOf() Method

Definition and Usage

The lastIndexOf() method returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string.

Syntax

stringObject.lastIndexOf(searchvalue,fromindex)

Parameter Description
search Required. Specifies a string value to search for
fromindex Optional. Specifies where to start the search. Starting backwards in the string


Tips and Notes

Note: The lastIndexOf() method is case sensitive!
Note: This method returns -1 if the string value to search for never occurs.

Example

In this example we will do different searches within a "Hello world!" string:

<script type="text/javascript">
var str="Hello world!";
document.write(str.lastIndexOf("Hello") + "<br />");
document.write(str.lastIndexOf("World") + "<br />");
document.write(str.lastIndexOf("world"));
</script>

The output of the code above will be:

0
-1
6
JavaScript lastIndexOf() Method Reviewed by 1000sourcecodes on 02:05 Rating: 5
Powered by Blogger.