Empty arrays seem to equal true and false at the same time

前端 未结 9 2139
别跟我提以往
别跟我提以往 2020-11-22 10:23

Empty arrays are true but they\'re also equal to false.

9条回答
  •  清酒与你
    2020-11-22 11:16

    Example:

    const array = []
    const boolValueOfArray = !!array // true
    

    It happens because

    ToNumber(ToPrimitive([])) == ToNumber(false)  
    
    1. [] is empty Array object → ToPrimitive([]) → "" → ToNumber("")0
    2. ToNumber(false) → 0
    3. 0 == 0 → true

提交回复
热议问题