String.indexOf()
AD · 728×90
Google AdSense / Яндекс.Директ
String
String.indexOf()
Returns the index of the first occurrence of a substring, or -1 if not found
→ numberPHP: strpos()ES1+
stringstring
Source string
searchValuestring
Substring to search for
fromIndexnumberoptional
Position to start searching from
(string).indexOf(searchValue, fromIndex)
RESULT
— click Run or press Ctrl+Enter —
Parameter Reference
ParameterTypeStatusDescription
stringstringrequiredSource string
searchValuestringrequiredSubstring to search for
fromIndexnumberoptionalPosition to start searching from
About

indexOf() returns the position of the first occurrence of a substring, or -1 if not found. The optional fromIndex specifies where to start. Case-sensitive. For a simple presence check, includes() is more readable and does not require comparing with -1.

Browser Support

Available since ES1 (1997). In ES7 (2016), includes() was added as a more readable alternative when position is not needed.

Tips & Gotchas