The lastIndexOf() Function is used to find the position of the last occurrence of the substring.
<html> <body> <script> var x = "The Beautiful world is Beautiful indeed" var y = x.lastIndexOf("Beautiful") document.write("The substring is located at the position ",y) </script> </body> </html>
In the above code, we have declared a String Hello Beautiful World and assigned it to a variable x.
var x = "Hello Beautiful World"
And we would be searching the second occurence of the substring Beautiful, from the String, The Beautiful world is Beautiful indeed.
So, we have used the lastIndexOf() Function to find the substring Beautiful.
var y = x.lastIndexOf("Beautiful")
And what lastIndexOf() Function does is, goes to the String The Beautiful world is Beautiful indeed and searches for the position of the last occurence of the substring Beautiful.
And finds the substring Beautiful in the 23trd position. And the position (i.e. 23) is stored in variable y.
And thus prints the position.
document.write("The substring is located at the position ",y)