The JavaScript indexOf() method is used to determine if a string contains a given substring. It searches for the substring in a string and returns the index of the first such occurrence of the specified string. If the substring is not found, the indexOf() method returns -1.

The indexOf() method is case-sensitive, which means it treats both uppercase and lowercase characters differently.

Syntax

str.indexOf(searchValue[, fromIndex])

Parameters

JavaScript indexOf() method takes two parameters:

  • searchValue — A string that represents the value to search for within this string. If no string is explicitly specified, searchValue will default to undefined and this value will be searched in the current string.
  • fromIndex — An optional integer that represents the index to start the search from. The default value is 0.

Return Value

The index of the first occurrence of searchValue in the string, or -1 if not found. If an empty string is passed as searchValue, it will match at any index between 0 and str.length.

Examples

In JavaScript, the first character in a string has an index of 0, and the index of the last character is str.length - 1. The indexOf() method gives the index of the string where the given substring is found for the first time.

Let us have an example:

const str = 'React is a popular JavaScript library.';

str.indexOf('React');        // 0
str.indexOf('Preact');       // -1
str.indexOf('React', 5);     // -1
str.indexOf('');             // 0
str.indexOf('', 5);          // 5

The indexOf() method is case-sensitive, which means the following expressions evaluate to -1:

'JavaScript'.indexOf('script');    // -1
'JavaScript'.indexOf('SCRIPT');    // -1

If more than one matched substrings are present, indexOf() will return the first position of the first occurrence:

'Java, Java, Java!'.indexOf('Java');   // 0

The value 0, returned by the indexOf(), method does not evaluate to true. Same is the case with -1 which does not evaluate to false either. So if you leave the equal operator to explicitly verify the presence of the string, you might see incorrect results.

Here is an interesting example:

const str = 'Apple is the manufacturer of iPhone X.';

if(str.indexOf('Apple')) {
    console.log('Awesome!');
}

if(str.indexOf('Samsung')) {
    console.log('Good!');
}

In the above code, the first if condition will never execute even though the word Apple does exist in the string. Similarly, the second if condition will evaluate to true although the string does not contain the word Samsung.

For this reason, when checking if a string contains another substring, the correct way to check would be:

if (str.indexOf('Apple') !== -1) {
    console.log('Awesome!');
}

if (str.indexOf('Samsung') !== -1) {
    console.log('Good!');
}

Read Next: 6 ways to check if a string contains a substring in JavaScript to learn about other JavaScript functions that can be used to check the presence of a substring in a string.

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.