String.prototype.indexOf()
indexOf(searchString, fromIndex?) Returns:
number · Added in ves6 · Updated March 13, 2026 · String Methods javascript es6 string search
The indexOf() method searches for the first occurrence of a substring within a string, starting from the beginning. It returns the index position where the substring starts, or -1 if the substring is not found. This is one of the most commonly used string methods in JavaScript for finding substrings.
Syntax
string.indexOf(searchString)
string.indexOf(searchString, fromIndex)
searchString: The substring to search for. If omitted,indexOf()returns0when searching for an empty string, or-1otherwise.fromIndex(optional): The index to start searching from. Defaults to0. Negative values are treated as0.- Returns: A number representing the index of the first occurrence, or
-1if not found.
TypeScript Signature
interface String {
indexOf(searchString: string, fromIndex?: number): number;
}
Basic Examples
const text = "hello world";
console.log(text.indexOf("hello")); // 0 (starts at index 0)
console.log(text.indexOf("world")); // 6
console.log(text.indexOf("!")); // -1 (not found)
console.log(text.indexOf("o")); // 4 (first occurrence)
Finding Multiple Occurrences
To find all occurrences of a substring, you need a loop:
const text = "hello hello hello";
const search = "hello";
let positions = [];
let idx = text.indexOf(search);
while (idx !== -1) {
positions.push(idx);
idx = text.indexOf(search, idx + 1);
}
console.log(positions); // [0, 6, 12]
Using the fromIndex Parameter
The second parameter controls where the search starts:
const text = "hello world hello";
console.log(text.indexOf("hello")); // 0 (searches from start)
console.log(text.indexOf("hello", 1)); // 6 (skips first occurrence)
console.log(text.indexOf("world", 10)); // -1 (starts past where world is)
Practical Use Case: Checking for Keywords
A common pattern is to check if a string contains a word or phrase:
function containsKeyword(text, keyword) {
return text.indexOf(keyword) !== -1;
}
console.log(containsKeyword("JavaScript is great", "Script")); // true
console.log(containsKeyword("JavaScript is great", "Type")); // false
Case Sensitivity
The indexOf() method is case sensitive:
const text = "JavaScript JavaScript";
console.log(text.indexOf("Java")); // 0
console.log(text.indexOf("java")); // -1 (lowercase does not match)
console.log(text.indexOf("SCRIPT")); // -1 (uppercase does not match)
// For case-insensitive search, convert to lowercase first
const lower = text.toLowerCase();
console.log(lower.indexOf("java")); // 0
Case-Insensitive Search Helper
function indexOfIgnoreCase(text, search) {
return text.toLowerCase().indexOf(search.toLowerCase());
}
console.log(indexOfIgnoreCase("Hello WORLD", "hello")); // 0
console.log(indexOfIgnoreCase("Hello WORLD", "WORLD")); // 6
Edge Cases
const text = "hello";
// Empty string returns fromIndex (or 0)
console.log(text.indexOf("")); // 0
console.log(text.indexOf("", 3)); // 3
// Negative fromIndex is treated as 0
console.log(text.indexOf("h", -5)); // 0
// fromIndex greater than string length
console.log(text.indexOf("h", 100)); // -1
// Search string longer than target
console.log(text.indexOf("hello world")); // -1
indexOf() vs includes()
| Method | Returns | Use Case |
|---|---|---|
indexOf() | number (index or -1) | When you need the position |
includes() | boolean | When you only need to check existence |
const text = "hello world";
console.log(text.indexOf("world")); // 6 (found, returns position)
console.log(text.includes("world")); // true (just checks existence)
Browser Compatibility
| Browser | Version |
|---|---|
| Chrome | 1+ |
| Firefox | 1+ |
| Safari | 1+ |
| Edge | 12+ |
| Node.js | 0.1.0+ |
Universal support across all modern and legacy JavaScript environments.
See Also
- string::lastIndexOf — Find the last occurrence of a substring
- string::includes — Check if a string contains a substring
- string::search — Search with regular expressions