Web/JavaScript/Reference/Global objects/String/indexOf
The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Note: For the Array method, see Array.prototype.indexOf().
Syntax
str.indexOf(searchValue [, fromIndex])
Parameters
searchValueThe string value to search for.
If no string is explicitly provided,
searchValuewill be coerced to "undefined", and this value will be searched for instr.So, for example:
'undefined'.indexOf()will return0, asundefinedis found at position0in the stringundefined.'undefine'.indexOf()however will return-1, asundefinedis not found in the stringundefine.fromIndexOptionalAn integer representing the index at which to start the search. Defaults to
0.For
fromIndexvalues lower than0, or greater thanstr.length, the search starts at index0andstr.length, respectively.For example,
'hello world'.indexOf('o', -5)will return4, as it starts at position0, andois found at position4. On the other hand,'hello world'.indexOf('o', 11)(and with anyfromIndexvalue greater than11) will return-1, as the search is started at position11, a position after the end of the string.
Return value
The index of the first occurrence of searchValue, or -1 if not found.
An empty string searchValue produces strange results. With no fromIndex value, or any fromIndex value lower than the string's length, the returned value is the same as the fromIndex value:
'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 any fromIndex value equal to or greater than the string's length, the returned 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, JS seems to find an empty string just after the specified index value. In the latter instance, JS seems to be finding an empty string at the end of the searched string.
Description
Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character of a string called stringName is stringName.length - 1.
'Blue Whale'.indexOf('Blue') // returns 0
'Blue Whale'.indexOf('Blute') // 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
The indexOf() method is case sensitive. For example, the following expression returns -1:
'Blue Whale'.indexOf('blue') // returns -1
Checking occurrences
Note that 0 doesn't evaluate to true and -1 doesn't evaluate to false. Therefore, when checking if a specific string exists within another string, the correct way to check would be:
'Blue Whale'.indexOf('Blue') !== -1 // true
'Blue Whale'.indexOf('Bloe') !== -1 // false
~('Blue Whale'.indexOf('Bloe')) // 0, which is falsy
Examples
Using indexOf()
The following example uses indexOf() to locate values in the string "Brave new world".
const str = 'Brave new world'
console.log('Index of first w from start is ' + str.indexOf('w')) // logs 8
console.log('Index of "new" from start is ' + str.indexOf('new')) // logs 6
indexOf() and case-sensitivity
The following example defines two string variables.
The variables contain the same string, except that the second string contains uppercase letters. The first console.log() method displays 19. But because the indexOf() method is case sensitive, the string "cheddar" is not found in myCapString, so the second console.log() method displays -1.
const myString = 'brie, pepper jack, cheddar'
const myCapString = 'Brie, Pepper Jack, Cheddar'
console.log('myString.indexOf("cheddar") is ' + myString.indexOf('cheddar'))
// logs 19
console.log('myCapString.indexOf("cheddar") is ' + myCapString.indexOf('cheddar'))
// logs -1
Using indexOf() to count occurrences of a letter in a string
The following example sets count to the number of occurrences of the letter e in the string str:
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) // displays 4
Specifications
| Specification |
|---|
| ECMAScript (ECMA-262)The definition of 'String.prototype.indexOf' in that specification. |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
indexOf
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1 |
IE
Full support 3 |
Opera
Full support 3 |
Safari
Full support 1 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
nodejs
Full support 0.1.100 |
Legend
- Full support
- Full support
See also
String.prototype.charAt()String.prototype.lastIndexOf()String.prototype.includes()String.prototype.split()Array.prototype.indexOf()
String.prototype.indexOf() by Mozilla Contributors is licensed under CC-BY-SA 2.5.