Get First Index of Occurrence of Specified Value in String in JavaScript



To get the first index of an occurrence of the specified value in a string, use the JavaScript indexOf() method.

Example

You can try to run the following code to get the first index −

Live Demo

<html>
   <head>
      <title>JavaScript String indexOf() Method</title>
   </head>

   <body>
      <script>
         var str = new String( "Learning is fun! Learning is sharing!" );
         var index = str.indexOf( "Learning" );
         document.write("First index of string Learning :" + index );
      </script>
   </body>
</html>
Updated on: 2020-06-15T06:59:06+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements