String.prototype.indexOf()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheindexOf()
method ofString
values searches this string and returns the index of the first occurrence of the specified substring. It takes an optional starting position and returns the first occurrence of the specified substring at an index greater than or equal to the specified number.
In this article
Try it
const paragraph = "I think Ruth's dog is cuter than your dog!";const searchTerm = "dog";const indexOfFirst = paragraph.indexOf(searchTerm);console.log(`The index of the first "${searchTerm}" is ${indexOfFirst}`);// Expected output: "The index of the first "dog" is 15"console.log( `The index of the second "${searchTerm}" is ${paragraph.indexOf( searchTerm, indexOfFirst + 1, )}`,);// Expected output: "The index of the second "dog" is 38"
Syntax
indexOf(searchString)indexOf(searchString, position)
Parameters
searchString
Substring to search for. All values arecoerced to strings, so omitting it or passing
undefined
causesindexOf()
to search for the string"undefined"
, which is rarely what you want.position
OptionalThe method returns the index of the first occurrence of the specified substring at a position greater than or equal to
position
, which defaults to0
. Ifposition
is greater than the length of the calling string, the method doesn't search the calling string at all. Ifposition
is less than zero, the method behaves as it would ifposition
were0
.'hello world hello'.indexOf('o', -5)
returns4
— because it causes the method to behave as if the second argument were0
, and the first occurrence ofo
at a position greater or equal to0
is at position4
.'hello world hello'.indexOf('world', 12)
returns-1
— because, while it's true the substringworld
occurs at index6
, that position is not greater than or equal to12
.'hello world hello'.indexOf('o', 99)
returns-1
— because99
is greater than the length ofhello world hello
, which causes the method to not search the string at all.
Return value
The index of the first occurrence ofsearchString
found, or-1
if not found.
Return value when using an empty search string
Searching for an empty search string produces strange results. With no second argument, or with a second argument whose value is less than the calling string's length, the return value is the same as the value of the second argument:
"hello world".indexOf(""); // returns 0"hello world".indexOf("", 0); // returns 0"hello world".indexOf("", 3); // returns 3"hello world".indexOf("", 8); // returns 8
However, with a second argument whose value is greater than or equal to the string's length, the return value is the string's length:
"hello world".indexOf("", 11); // returns 11"hello world".indexOf("", 13); // returns 11"hello world".indexOf("", 22); // returns 11
In the former instance, the method behaves as if it found an empty string just after the position specified in the second argument. In the latter instance, the method behaves as if it found an empty string at the end of the calling string.
Description
Strings are zero-indexed: The index of a string's first character is0
, and the index of a string's last character is the length of the string minus 1.
"Blue Whale".indexOf("Blue"); // returns 0"Blue Whale".indexOf("Wale"); // returns -1"Blue Whale".indexOf("Whale", 0); // returns 5"Blue Whale".indexOf("Whale", 5); // returns 5"Blue Whale".indexOf("Whale", 7); // returns -1"Blue Whale".indexOf(""); // returns 0"Blue Whale".indexOf("", 9); // returns 9"Blue Whale".indexOf("", 10); // returns 10"Blue Whale".indexOf("", 11); // returns 10
TheindexOf()
method is case sensitive. For example, the followingexpression returns-1
:
"Blue Whale".indexOf("blue"); // returns -1
Checking occurrences
When checking if a specific substring occurs within a string, the correct way to check is test whether the return value is-1
:
"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'"Blue Whale".indexOf("Wale") !== -1; // false; no 'Wale' in 'Blue Whale'
Examples
>Using indexOf()
The following example usesindexOf()
to locate substrings in the string"Brave new world"
.
const str = "Brave new world";console.log(str.indexOf("w")); // 8console.log(str.indexOf("new")); // 6
indexOf() and case-sensitivity
The following example defines two string variables.
The variables contain the same string, except that the second string contains uppercaseletters. The firstconsole.log()
method displays19
. Butbecause theindexOf()
method is case sensitive, the string"cheddar"
is not found inmyCapString
, so the secondconsole.log()
method displays-1
.
const myString = "brie, pepper jack, cheddar";const myCapString = "Brie, Pepper Jack, Cheddar";console.log(myString.indexOf("cheddar")); // 19console.log(myCapString.indexOf("cheddar")); // -1
Using indexOf() to count occurrences of a letter in a string
The following example setscount
to the number of occurrences of thelettere
in the stringstr
:
const str = "To be, or not to be, that is the question.";let count = 0;let position = str.indexOf("e");while (position !== -1) { count++; position = str.indexOf("e", position + 1);}console.log(count); // 4
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-string.prototype.indexof> |
Browser compatibility
Loading…