[removed] the confuse about comparison “2 == true”

前端 未结 2 1184
我在风中等你
我在风中等你 2021-01-20 10:11

Here is a javascript comparison:

2 == true //false

it\'s said, the reason why return false, is because the comparison convert the tru

2条回答
  •  不思量自难忘°
    2021-01-20 10:31

    == does implicit conversion to compare. In this case 2 is number and true is boolean. The conversion rule is "while comparing a number to boolean, boolean will be converted to number" hence

    true is converted to 1

    and 2 == 1 will be false.

    //similarly, 
    2 == false; //false
    

    As false will be converted to 0 and 2 cannot be equal to 0 either.

    However, 1 == true. for the same reason as true would be converted to 1 and 1==1

提交回复
热议问题