The JavaScript string indexOf() method retrieves the position of a char value present in the given string. It is case-sensitive.
Syntax 1:
string.indexOf(char)
Syntax 2:
string.indexOf(ch,index)
Syntax 3:
string.indexOf(str)
Syntax 4:
string.indexOf(str,index)
Parameters:
char: It represents the specified char like “c”.
str: It represents the specified string like “w3schools”.
index: It represents the specified position from where the search has to start.
Return:
It will return the position of a char value if a char is present in the string otherwise returns -1.
Example:
<!DOCTYPE html> <html> <body> <script> var a ="Best tutorial website: w3schools"; document.write(a.indexOf('e')); document.write("</br>"); document.write(a.indexOf('e', 4)); document.write("</br>"); document.write(a.indexOf("es")); document.write("</br>"); document.write(a.indexOf("es", 4)); </script> </body> </html>