AD · 728×90
Google AdSense / Яндекс.Директ
Google AdSense / Яндекс.Директ
String
String.indexOf()
Returns the index of the first occurrence of a substring, or -1 if not found
Source string
Substring to search for
Position to start searching from
(string).indexOf(searchValue, fromIndex)
RESULT
— click Run or press Ctrl+Enter —
Parameter Reference
| Parameter | Type | Status | Description |
|---|---|---|---|
| string | string | required | Source string |
| searchValue | string | required | Substring to search for |
| fromIndex | number | optional | Position 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
- For a presence check, includes() is more readable than indexOf() !== -1
- fromIndex can be negative in lastIndexOf()
- to find all occurrences, use a loop or matchAll() with a RegExp.