Usage
indexOf() returns the position of element from the elements.
|
Syntax
String.indexof("element",startingPosition)
By default - starting position is -1 , if the position not defined.
|
var userName = "Prasanna";
//The counting of element position start from -1 up to finding the first 'a' element.
var char1 = userName.indexOf("a");
document.write("The element position is "+char1+" in the string.");
//The counting of element position start from 2 that is count 2 + Actual count(2) of the element 'n'.
var char2 = userName.indexOf("a",2);
document.write("The element position is "+char2+" in the string.");
|
Output
The element position is 2 in the string.
The element position is 4 in the string.
|