Why do alert(!!“0”) and alert(false == “0”) both output true in JavaScript

后端 未结 3 1292
别跟我提以往
别跟我提以往 2020-12-06 11:52

As far as I know in JavaScript !! is supposed to normalize a boolean value converting it to true or false from some other type. This would mean that the \"0\" converts to bo

3条回答
  •  有刺的猬
    2020-12-06 12:10

    In the first case, a non-empty string is equivalent to true.

    In the second case, because one operand is a boolean, both operands are converted to numeric values. I believe false converts to the numeric value 0 and the string "0" also converts to a numeric 0, resulting in 0 == 0 which is true.

    Check out the Mozilla reference for operator behavior.

提交回复
热议问题