Learn JavaScript
String Methods
17.7
Lesson

Finding Index of Character in String

There are a couple of ways in which we can find if a string contains another string.

The indexOf() method helps us locate a string inside another.

The method accepts two arguments - a substring and the position. The method looks for the substring starting from the position inside the main string. It returns the position at which the string gets matched first.

In the example given above, we try to locate the index of the string es inside the string Sam loves mangoes.. Once without a starting index, and once with a starting index.

If there are no matches, the method would return -1.