Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't co

Posted by Mariano Peterson on Stack Overflow See other posts from Stack Overflow or by Mariano Peterson
Published on 2010-06-18T03:40:02Z Indexed on 2010/06/18 3:43 UTC
Read the original article Hit count: 253

Filed under:
|
|

For example, this returns true, and makes sense: var x = [1,2]; 1 in x; // true

This returns false, and makes sense: var x = [1,2]; 3 in x; // false

However this returns true, and I don't understand why: var x = [1,2]; 0 in x;

You can quickly test it by putting this in your browser's address bar: javascript:var x=[1,2]; alert(0 in x);

Why does the "in" operator in Javascript return true when testing if "0" exists in array, even when the array doesn't appear to contain "0"?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about array