JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow String Right Arrow lastIndexOf()

lastIndexOf()  String  instance method

Description

Returns the index value for the last occurence of the specified string from the specified starting index.

  • Returns -1 if the specified string value is not found.

Syntax


Signature Description
aString.lastIndexOf(searchValue[, fromIndex])Returns the index value for the first occurence of the specified string from the specified starting index.

Parameters


Parameter Description
searchValueA string value.
fromIndexAn integer between 0 and length of the string that defaults to the length of the string.
  • If this parameter is specified the string is searched backwards from this point to index 0.

Examples

The code below creates a repeating string and searches backwards through it as both the searchValue and fromIndex parameters are specified.


// Create alphabet string variable.
var repeatingString = 'abcdefabcdefabcdefabcdef';
// Return index of last occurrence of search string.
alert(repeatingString.lastIndexOf('ab', 14));

Press the button below to action the above code:

Related Tutorials

JavaScript Basic Tutorials - Lesson 8 - Strings



  

go to home page Homepage go to top of page Top