Why does !{}[true] evaluate to true in JavaScript?

前端 未结 10 1465
一个人的身影
一个人的身影 2020-12-12 17:55

{}[true] is [true] and ![true] should be false.

So why does !{}[true] evaluate to true

10条回答
  •  隐瞒了意图╮
    2020-12-12 18:22

    The answers here are good, here's a breakdown in pseudo-code:

    • {}['whatever'] = empty block, NewArray('whatever') = NewArray('whatever')
    • {}[true] = empty block, NewArray(true) = NewArray(true)
    • !{}['whatever'] = LogicalNOT(convertToBool(NewObject.whatever)) = LogicalNOT(convertToBool(undefined)) = LogicalNOT(false) = true
    • ({}['whatever']) = Grouping(NewObject.whatever) = Grouping(undefined) = undefined

提交回复
热议问题