Knowledge Walls
Laya
Hyderabad, Andhra Pradesh, India
String.prototype.indexOf() Method in String Class of Javascript Syntax with Examples
3388 Views
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.
Example
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.
Best Lessons of "Javascript Syntax with Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details