Why does javascript’s “in” operator return true when testing if 0 exists in an array that doesn’t contain 0?

It refers to the index or key, not the value. 0 and 1 are the valid indices for that array. There are also valid keys, including "length" and "toString". Try 2 in x. That will be false (since JavaScript arrays are 0-indexed).

See the MDN documentation.

Leave a Comment