How To Use String Object lastIndexOf In Java Script

In this article I am going to explain about String Object lastIndexOf In JavaScript.
  • 1488

Use lastIndexOf() method in JavaScript

The indexOf() method find the last position of string value from the given character sets. 

The indexOf() method returns -1 if the value to search for never occurs, and start the end of character set.

This method is case sensitive.

All browsers support string object lastIndexOf method.

Syntax

string.lastIndexOf(searchvalue,start)

Example

<!DOCTYPE html>

<html>

<body style ="background-color:Red">

 

<p id="demo">To locate the last occurance of a specified value.</p>

 

<button onclick="myFunction()">Click here</button>

 

<script type="text/javascript">

    function myFunction()

     {

        var str = "Hi welcome to india.";

        var n = str.lastIndexOf("india");

        document.getElementById("demo").innerHTML = n;

    }

</script>

 

</body>

</html>

Output1

 op1.jpg

Output2

 op2.jpg

Further Readings

You may also want to read these related articles :here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.