javascript operator “in”

前端 未结 5 925
渐次进展
渐次进展 2021-01-25 00:56

I\'m used to python, so

a = [1,2,3]
1 in a # -> True
b = [\"1\", \"2\", \"3\", \"x\"]
\"x\" in b # -> True

Why is it that in JavaScript

5条回答
  •  终归单人心
    2021-01-25 01:17

    in works on KEYS of arrays, not values. 1 in a succeeds because there is an element #1 in your array, which is actually the 2 value.

    "1" fails, because there is no 1 PROPERTY or KEY in your array.

    Details here: https://developer.mozilla.org/en/JavaScript/Reference/Operators/in

提交回复
热议问题