String.indexOf() Find a substring index
Last updated: 2026-09-09
String.indexOf() Find a substring index
indexOf returns the index of the first occurrence, or -1. Case-sensitive.
| Category | Strings |
|---|---|
| ES version | ES1 |
📝 Syntax
const index = str.indexOf(substring, position);
⚙️ Parameters
| substring | The substring to find. |
|---|---|
| position | Optional. Start position, default 0. |
Returns:First index, or -1.
Mutates the original:No (returns a new value)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
6
-1
💡 Tip indexOf always returns 0 for empty substring.
ℹ️ info Loop: while ((pos = str.indexOf(sub, pos + 1)) !== -1).
🌐 Browser support
| Browser | |||||
|---|---|---|---|---|---|
| String.indexOf() | ✓ v1 | ✓ v12 | ✓ v1 | ✓ v1 | ✓ v1 |
| Supported by all modern browsers | |||||
🏷️ Related Items
💡 Related Cases
More case studies coming soon — check back later.
📚 Related Tutorials
❓ FAQ
QWhat does -1 mean?
ANot found. -1 is truthy — always check !== -1.
QHow to search case-insensitively?
Atext.toLowerCase().indexOf(keyword.toLowerCase()).
QHow to find the last occurrence?
AUse lastIndexOf().