1. convert using a Boolean object
Copy Code code as follows:
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 '! ' operator, the first '! ' Converts a value to a Boolean value and takes a value that is not a value, the second '! ' Restore its Boolean value, similar to the "negative negative positive" principle.
Copy Code code as follows:
var num123 = 123, str = ' abc ', o = {name: ' Test '}, NUM0 = 0; num123 =!! (num123); True NUM0 =!! (NUM0); False str =!! (str); True o =!! (o); True
The results obtained are the same as Method 1.
Finally, you should know that any JavaScript value can be converted to a Boolean value. The following values are converted to false:
Copy Code code as follows:
Undefined,null,0,-0,nan, ""//empty string
need to be awareIs ' 0 ', which contains only 0 of the strings that turn into true!.
Add the specificity of null and undefined:
Copy Code code as follows:
Null = = undefined//true null = = undefined//false