Why does IndexOf return -1?

Because arrays are 0 based, returning 0 would mean starting from the first character was matched; 1, the second character, and so on. This means anything 0 and up would be a true or "found" response. To keep everything in the integer category, -1 signifies no match found.


-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.


-1 means no match found. "good" is not in that sentence. This is documented behaviour.

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.