JavaScript indexOf substring
In this section, we are going to find the location of substring using the JavaScript method indexOf(). When this method is called from the string object, it returns the index of the position where the specified string first occurs in a string. If the particular string is not found then it will return -1. You can see in the given code, we have used the string.indexOf(substring,0) method where the parameters substring is the specified string and 0 is the index that will allow to start the search for the specified substring.
Here is the code:
| <html> <h2>Use of indexOf() method</h2> <script type="text/javascript"> var string = "Hello World"; var substring = "World"; var position= string.indexOf(substring,0); document.write("<b>The substring " + substring + " is at position </b>" + position); </script> </html> |
Output will be displayed as:

Tutorials
- Clear cookie example
- JavaScript getElementById innerHTML
- JavaScript getElementById Style
- Javascript Examples
- JavaScript add row dynamically to table
- New Page 1
- JavaScript Change link
- JavaScript Checkbox getElementById
- javascript clear textarea
- JavaScript Clock Timer
- JavaScript Cookies
- JavaScript Date Difference
- JavaScript duplicate string
- JavaScript Email Validation
- javascript focus input
- JavaScript get excel file data
- JavaScript getAttribute Href
- JavaScript getAttribute Style
- JavaScript getElementById div
- JavaScript getElementById Iframe
- JavaScript getElementById select
- JavaScript Hide Button
- JavaScript Hide Div
- JavaScript hide image
- JavaScript Hide Table Column
- JavaScript Hide Table Rows
- JavaScript Key Event
- JavaScript link
- JavaScript method location
- JavaScript move div
- JavaScript move file
- JavaScript move image
- JavaScript Navigate Back
- JavaScript navigate to page
- JavaScript Navigate to URL
- JavaScript indexOf substring
- JavaScript onkeypress event
- JavaScript Open file
- JavaScript Open link in new window
- JavaScript Open Modal Window


