Plz help me understand the string character here

i just came across indexof() method and these two code left me scratching my head…

<h1>  Code 1 </h1>

var str="where does this word occurs!";
        var pos =str.indexOf("word");

<h1> code 2 </h1>

 var str = "Please locate where locate occurs!";
        var pos = str.indexOf("locate");
        document.getElementById("demo").innerHTML = pos;

The first one gives me the position of 16 and the second one 7.For the first one,if i count from 0 to the “word” by coutning letters, it abruptly ends in the space before the "word"occurs.

for the second one,if i count by words it ends in pos 7.

i’m still gazing at this dazed and confused.haha.

strings are 0-indexed (the firet character is at index 0)
in the first case the word start at the 17th character which is at index 16
in the second case the first instance of the word starts at the 8th character, which is at index 7

1 Like

Ya,i just realized i have been cycling around the second word “locate” in the second code for last 2 hours.Guess,it was the all the fear revolving around learning JS over the web.Thank you very much tho!