How to work out values in javascript

The string is treated as if it were an array of characters and the indexOf looks for the first occurrence of the provided string. If it does not find it then it returns -1 to indicate not found.

var str="Hello World"
s = str.indexOf("o");
console.log(s); //4th position - indexes are begin at zero - so its fifth in the array.
s = str.indexOf("w");
console.log(s); //-1 - not found
s = str.indexOf("r");
console.log(s); //8
s = str.lastIndexOf("l");
console.log(s); //9

Browse More Popular Posts

Leave a Comment