JavaScript String Method : indexOf(str)

The indexOf() method returns the index position of the first occurrence of given value in a string. The syntax would be :
  str.indexOf("value");
Example :
<html>
<body>
  <script>
    var mystr = "This is the Test String for the indexOf() method.";
    var pos = mystr.indexOf("the"); 
    document.write("Position of 'The' is : ", pos);
  </script> 
</body>
</html>
Output :


If the provided value is not found then it will return -1.

Next Topic :