JavaScript String indexOf() Tutorial

In this section, we will learn what the indexOf() method is and how to use it in JavaScript.

JavaScript String indexOf() Method

The indexOf() method is used in JavaScript to search for a string value for a substring. If the target string had this substring value, then it will return the index number (the position) of that substring. Otherwise the number -1 will return (which means this string value does not have the substring we’re looking for)

JavaScript String indexOf() Syntax

string.indexOf(searchvalue, start)

String indexOf() Method Parameters:

The `indexOf()` method takes two arguments:

  • The first argument is the value that we want to find its location in the target string.
  • The second argument is the location (index number) from which we want this search to begin. The default value is set to 0, which means the search should begin from the start position of that string.

String indexOf() Method Return Value:

The indexOf() method returns the index number of the first occurrence of the value we set as its argument.


  • If the value does not exist in the target string, the return value will be -1.
  • The method is case sensitive. This means if the argument is “test” for example, but in the target string there’s a value “Test” with the first letter being capital, then the final result becomes -1.

Example: JavaScript find in string

const message = "The relationship between passion and determination is one-to-many!"






Top Technologies