Skip to content Skip to sidebar Skip to footer

Why Does ("a" In ["a","b"]) Yield False, And (1 In [1,2]) Yield True?

Possible Duplicate: javascript in operator Why does ('a' in ['a','b']) yield false, and (1 in [1,2]) yield true ? Is there a reason why 'a' does not match the first element of t

Solution 1:

The in operator checks for the existence of properties by key, not by value. And your array of length 2 has an index "1" - arr["1"] is the value 2. For example, also 0 in ["a", "b"] is true. The behaviour does not depend on a string or a number being used.

You usually would use it on plain objects, not on arrays. Like "a" in {a:1} === true, or "b" in {a:1} === false.

Post a Comment for "Why Does ("a" In ["a","b"]) Yield False, And (1 In [1,2]) Yield True?"