[removed] What is the difference between `if (!x)` and `if (x == null)`?

后端 未结 6 2028
遥遥无期
遥遥无期 2020-12-08 05:18

What is the difference between if (!x) and if (x == null); that is, when can their results be different?

6条回答
  •  独厮守ぢ
    2020-12-08 05:59

    if (!x) 
    

    coerces x uses the internal ToBoolean function

    if (x==null)
    

    coerces both operands using the internal ToPrimitive function (which generally resolves each side to a number, occasionally a string, depending on the operands)

    For full explanantion of ToBoolean vs ToPrimitive see http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

提交回复
热议问题