0 in [1, 2] == true, why?

javascript

Solution

Because "in" returns true if the specified property/index is available in the object. [1, 2] is an array, and has a object at the 0 index. Hence, 0 in [1, 2], and 1 in [1, 2]. But !(2 in [1, 2]).

Edit: For what you probably intended, David Dorward's comment below is very useful. If you (somewhat perversely) want to stick with 'in', you could use an object literal

x = {1: true, 2: true};

This should allow `1 in x && 2 in x && !(0 in x)` etc. But really, just use indexOf.

Problem

Excerpt from my JavaScript console: ``` > 0 in [1, 2] true ``` Why?

Original source