JavaScript Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Java-Script Tutorial
Introduction Introduction
Variables Variables
Operators Operators
Statements Statements
Loops Loops
Functions Functions
Objects Objects
Arrays Arrays
Cookies JS Cookies
Browser Objects Browser Objects
Form DOM Form DOM
Style Objects DOM Style Objects
Forums Ask Your Doubts
Scraps More about Javascript
Feedback Feedback
 




String Function - lastIndexOf


Tutorials Javascript

Topic

how to find the last occurrence position of a character or word in a string using javascript?



Explanation

Object: String

Method or Function: lastIndexOf(string)

Description: This method is similar to indexOf() function and takes a character or string as an argument. Searches the given string in the main string and returns the last occurrence position of the string. Returns -1 if the search string is not present in the main string.


Example Code:
<script language=javascript>
var ss = "a last index test ";
var result = ss.lastIndexOf("e");
document.write(result);
</script>

Result:

Explanation:
In the above example,
  • the main string is stored in a variable ss.
  • the method take a character "e" as argument.
  • the method searches in main string (ss) for the search string "e".
  • "e" is at two positions 10 and 14. last occurrence value 14 is returned as result.





  • A Note
    Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script. Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.

    Other Links

    web hosting