1.Convert using Boolean objects
var num123 = 123, str = 'abc', o = {name:'test'}, num0 = 0; num123 = Boolean(num123); //true num0 = Boolean(num0); //false str = Boolean(str); //true o = Boolean(o); //true
2. Use two '!' operators, the first '!' converts the value into a Boolean value and takes the non-value of its value, the second '!' converts its Boolean value Reduction is similar to the principle of "a negative makes a positive".
var num123 = 123, str = 'abc', o = {name:'test'}, num0 = 0; num123 = !!(num123); //true num0 = !!(num0); //false str = !!(str); //true o = !! (o); //true
The result obtained is the same as method 1.
Finally, you should know : Any JavaScript value can be converted into a Boolean value. The following values will be converted to false:
undefined,null ,0,-0,NaN,"" //Empty string
It should be noted that is '0', a string that only contains 0 will be converted to true!
Add the special features of null and undefined:
null == undefined //true null === undefined //false