Array.indexOf() Find the index of a value
Last updated: 2026-09-09
Array.indexOf() Find the index of a value
indexOf returns the index of the first occurrence, or -1. It compares strictly and cannot match NaN.
| Category | Arrays |
|---|---|
| ES version | ES3 (1999) |
📝 Syntax
array.indexOf(searchValue, fromIndex);
⚙️ Parameters
| searchValue | The value to find, compared with ===. |
|---|---|
| fromIndex | Optional. Start position; negative counts from the end. |
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:
1
-1
found at 1
found at 3
⚠️ Warning indexOf uses === so it cannot match NaN — use includes or findIndex.
💡 Tip Prefer includes for existence; indexOf earns its keep when you need the position.
🌐 Browser support
| Browser | |||||
|---|---|---|---|---|---|
| Array.indexOf() | ✓ v1 | ✓ v12 | ✓ v1.5 | ✓ v3 | ✓ v10.5 |
| Supported by all modern browsers | |||||
🏷️ Related Array Methods
💡 Related Cases
More case studies coming soon — check back later.
📚 Related Tutorials
❓ FAQ
QindexOf vs lastIndexOf?
AindexOf finds the first occurrence; lastIndexOf finds the last, searching backwards.
QWhy does indexOf fail on object arrays?
AObjects compare by reference. Use findIndex with a callback to match by property.
QIs indexOf strict?
AYes — === with no coercion; indexOf("1") does not match 1.