String indexOf() Method - Javascript String

Javascript examples for String:indexOf

Description

The indexOf() method returns the position of the first occurrence of a specified value in a string.

The indexOf() method is case sensitive.

Parameter Values

Parameter Description
searchvalue Required. The string to search for
start Optional. Default 0. At which position to start the search

Return Value:

A Number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never occurs

The following code shows how to Search a string for "welcome":

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/* ww  w .  j  a  v a 2 s  .  c  o  m*/
    var str = "Hello world test test test";
    var n = str.indexOf("e", 5);
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

Related Tutorials