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.


str.indexOf(searchValue[, fromIndex])


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.


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. The 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')) {

if(str.indexOf('Samsung')) {

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) {

if (str.indexOf('Samsung') !== -1) {

Read Next: 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. Subscribe to RSS Feed.

👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, ☕ Buy me a coffee (cost $5).