Excerpt from my JavaScript console:
> 0 in [1, 2]
true
Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
This should allow
1 in x && 2 in x && !(0 in x)etc. But really, just use indexOf.