![]() |
VOOZH | about |
The indexOf() method in TypeScript is an inbuilt function used to find the index of the first occurrence of a specified value within a calling string. If the value is not found, it returns -1.
string.indexOf(searchValue[, fromIndex]) Parameter: This method accepts two parameters as mentioned above and described below.
Return Value: This method returns the index of the found occurrence, otherwise -1 if not found.
The below example illustrates the String indexOf() method in TypeScriptJS:
In this example we finds the position of "world" in the string str using indexOf, which returns 21 because world starts at the 21st character (0-based index).
Output:
21In this example we searches for JavaScript in str using indexOf, returning -1 because JavaScript is not found within the string.
Output:
-1