English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript String indexOf() Method

 JavaScript String Object

indexOf()Returns the position of the first occurrence of the specified value in the string.

If the value is not found, it will return-1.

If the value exists multiple times, it will return the position of the first occurrence.

If you want to search from the beginning, uselastIndexOf()method.

Note:For more information on Array methods, seeArray.indexOf().

Syntax:

string.indexOf(searchValue, start)
var str = 'Air Pollution is the introduction of chemicals to the atmosphere';
str.indexOf('Pollution');// 4
Test and see‹/›

Note:This method is case-sensitive.

Browser Compatibility

All browsers fully support the indexOf() method:

Method
indexOf()IsIsIsIsIs

Parameter Value

ParameterDescription
searchValue(Required) A string representing the value to search for
start(Optional) An integer representing the index at which to start the search; the default is 0

Technical Details

Return value:first occurrencesearchValueindex, if not found then-1
JavaScript version:ECMAScript 1

More examples

Return the position of the character 'L' in the string, starting from position6Start searching:

var str = 'HELLO WORLD HELLO';
str.indexOf('L', 6);
Test and see‹/›

 JavaScript String Object