Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
130
rated 0 times [  137] [ 7]  / answers: 1 / hits: 51672  / 13 Years ago, wed, december 21, 2011, 12:00:00

I am learning Javascript and don't understand why the indexOf below returns -1:



var string = The quick brown fox jumps over the lazy dog;

console.log (string.indexOf(good));

More From » indexof

 Answers
11

-1 means no match found.



The reason it returns -1 instead of false is that a needle at the beginning of the string would be at position 0, which is equivalent to false in Javascript. So returning -1 ensures that you know there is not actually a match.


[#88454] Monday, December 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zainsagez

Total Points: 555
Total Questions: 99
Total Answers: 96

Location: Honduras
Member since Sat, Jul 24, 2021
3 Years ago
;