Tutorials Tonight
Follow Us

JavaScipt String indexOf Method

This tutorial explains the indexOf string method in javascript to find the index of a string within a string.

String indexOf Method

The indexOf() method is used to find the index of the first occurrence of the specified value within the calling string.

If the specified value (string or character) exist within the calling string then it returns the index value of the first occurrence of the value and if no such value is found then it returns -1.


const quote = "Learn to code";
▶ Try It
javascript string indexof

Syntax of indexOf string method

The syntax of indexOf() method is as following:

string.indexOf(search_value [ ,start_index])

1. string - It is the calling string.

2. search_value - It is the string value you search for. If the search string is not given then the method searches for 'undefined' within the calling string.

Search value not given so search for 'undefined'

const quote = "Learning to code is learning to create and innovate";
const sentence = "Search for undefined";
console.log(quote.indexOf()); // search for 'undefined'
console.log('undefined'.indexOf()); // 'undefined' at index 0

3. start_index (optional) - It is the index value from where you want to search for the string. Its default value is 0.

Example with start index

const quote = "Learning to code is learning to create and innovate";
console.log(quote.indexOf("Learning", 5));
console.log(quote.indexOf("code", 10));
console.log(quote.indexOf("learning", 12));
▶ Try It

start_index value lower than 0 starts at index 0 and start_index value greater than string.length starts at index string.length and always return -1.

const quote = "Learning to code is learning to create and innovate";
console.log(quote.indexOf("create", -10));
console.log(quote.indexOf("code", 100));
console.log(quote.indexOf("learning", 60));
▶ Try It

In the above example when the index is -10 then it starts searching from index 0 and when the index is greater than string length then it always returns -1 since searching starts from string.length.


The indexOf method is case sensitive, which means it treats the same string with different cases as different.

const quote = "Learning to code is learning to create and innovate.";
console.log(quote.indexOf("learning")); // 20
console.log(quote.indexOf("Learning")); // 0
console.log(quote.indexOf("code")); // 12
console.log(quote.indexOf("Code")); // -1
▶ Try It

In the above example, the method searched for the same word ("learning") but with different cases. Because of case sensitivity the word "learning" appears at index 20 while "Learning" appears at index 0.

Strange return value

The indexOf() method returns strange value when search string is empty ('').

When the start index value is less than the string's length then it returns a value the same as the start index.

console.log("Learn to code".indexOf("")); // return 0 default value
console.log("Learn to code".indexOf("", 0)); // 0
console.log("Learn to code".indexOf("", 4)); // 4
console.log("Learn to code".indexOf("", 8)); // 8

However when the search string is empty and the starts index is greater than the string length then it always returns the value of string length.

console.log("Learn to code".indexOf("", 15)); // 13
console.log("Learn to code".indexOf("", 20)); // 13
console.log("Learn to code".indexOf("", 30)); // 13

Using the indexOf() method to count the occurrence of a letter in a string

Since we don't know the number of iterations to find the occurrence of a character using the indexOf method we can use a while loop and set a variable count for occurrences of the letter in the string.

const quote = "Learning to code is learning to create and innovate.";
let count = 0;
let index = quote.indexOf("a");
while (index !== -1) {
  index = quote.indexOf("a", ++index);

Difference between includes() and indexOf() string methods

The includes string method determines whether a string or a character can be found within a string or not and on the basis of that it returns true or false.

While the indexOf string method returns the index of the first occurrence of a given character or string.

Points to remember:

  1. The indexOf() returns the index of the first occurrence of a string within another string if no such occurrence is found then returns -1.
  2. The indexOf method is case-sensitive.
  3. Using the indexOf method you can find both string and character within a string.