0
someone briefly explain to me this code because i don't know why am getting 7
<!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <p>The indexOf() method returns the position of the first occurrence of a specified text:</p> <p id="demo"></p> <script> var str = "Please locate where 'locate' occurs!"; var pos = str.indexOf("locate"); document.getElementById("demo").innerHTML = pos; </script> </body> </html>
4 odpowiedzi
+ 2
Thinking of the string as an array (with "P" having an index of 0, "l" having an index of 1, etc.), counting up to the first letter in the first occurrence of the word "locate" results in the function returning 7 (the "l" in locate is located at the 7th index within the string).
+ 2
The JavaScript indexOf() method is the best way to find out if one string exists inside of another. If it does, the starting position of that searched string is returned. If the sub-string does not exist, then “-1” is returned; this is a reliable indicator of “does not exist”.
0
If you say, "var pos" meaning you want to identify the number of that term
Since for an array, we begin counting from '0' and therefore the pos of the term "locate " l is at 7
That's why the answer is 7.
0
means you start counting from index?