JavaScript String indexOf() is a built-in method that searches for the first occurrence of a specified substring within a string and returns its starting index. If it does not find a substring, it returns -1; otherwise, it returns the starting index. The indexOf() method is case-sensitive and zero-indexed.
const input_string = "App, Dividend!"; console.log(input_string.indexOf("Dividend")); // Output: 5
In this code, we are trying to find the starting index of the substring “Dividend”. Now, the first character of the main string is “A” and its index starts with 0.
Now, we want to find the index of “Dividend”, so its starting character is “D” and its index is 5. You also need to count “,” and “whitespace” as characters while scanning for the substring. Hence, the output is index 5.
Syntax
str.indexOf(search_string, from_index)
Parameters
Argument | Description |
search_string | It represents the substring to search for. |
from_index (optional) | It represents an index at which you can begin the search.
By default, its value is 0. If it is negative, it is treated as 0. |
With the starting position
If you pass the from_index as a starting index for searching, it will start searching the substring with that specific index.
const input_string = "App, Dividend!"; console.log(input_string.indexOf("dend", 6)); // Output: 9
In this code, we passed the from_index as index 6. Therefore, it will start searching from index 6 and proceed left-to-right. Since, it found the substring “dend” at starting index 9, it returns that index as an output.
Case sensitivity
The indexOf() is a strictly case-sensitive, and if the substring in an input string is “dend” and you are searching for “Dend”, it will return -1 because “Dend” does not exist in the main string.
const input_string = "App, Dividend!"; console.log(input_string.indexOf("Dend", 6)); // Output: -1
However, you can use string.toLowerCase() or regex for case-insensitive needs.
Empty search string
An empty string is considered found at any valid position, clamped between 0 and the length of the string.
const input_string = "App, Dividend!"; console.log(input_string.indexOf("")); // Output: 0 (default behavior) console.log(input_string.indexOf("", 2)); // Output: 2 console.log(input_string.indexOf("", 5)); // Output: 5
Position greater than length
If the starting position is greater than the length of the input string, it will return -1 as an output because it is not possible to find a substring outside of its length.
const input_string = "App, Dividend!"; console.log(input_string.indexOf("end", 20)); // Output: -1
Passing a negative position
If you pass a negative position, it searches from the start. If fromIndex (position) < 0, JavaScript treats it as 0.
const input_string = "App, Dividend!"; console.log(input_string.indexOf("dend", -10)); // Output: 9
Here, input_string.indexOf(“dend”, -10) is equivalent to input_string.indexOf(“dend”, 0).
The substring “dend” starts at index 9 and is therefore included in the output.
That’s all!