English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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().
string.indexOf(searchValue, start)
var str = 'Air Pollution is the introduction of chemicals to the atmosphere'; str.indexOf('Pollution');// 4Test and see‹/›
Note:This method is case-sensitive.
All browsers fully support the indexOf() method:
Method | |||||
indexOf() | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
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 |
Return value: | first occurrencesearchValueindex, if not found then-1 |
---|---|
JavaScript version: | ECMAScript 1 |
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‹/›