Home >Web Front-end >JS Tutorial >Basic use of Boolean objects in JavaScript programming_Basic knowledge
Boolean objects are used to convert non-Boolean values into Boolean values (true or false).
Check Boolean value
Checks whether a boolean object is true or false.
Source code example:
<!DOCTYPE html> <html> <body> ​ <script> var b1=new Boolean(0); var b2=new Boolean(1); var b3=new Boolean(""); var b4=new Boolean(null); var b5=new Boolean(NaN); var b6=new Boolean("false"); ​ document.write("0 is boolean "+ b1 +"<br>"); document.write("1 is boolean "+ b2 +"<br>"); document.write("An empty string is boolean "+ b3 + "<br>"); document.write("null is boolean "+ b4+ "<br>"); document.write("NaN is boolean "+ b5 +"<br>"); document.write("The string 'false' is boolean "+ b6 +"<br>"); </script> ​ </body> </html>
Test results:
0 is boolean false 1 is boolean true An empty string is boolean false null is boolean false NaN is boolean false The string 'false' is boolean true
Create Boolean object
Boolean object represents two values: "true" or "false"
The following code defines a Boolean object named myBoolean:
var myBoolean=new Boolean();
If the Boolean object has no initial value or its value is:
0 -0 null "" false undefined NaN
Then the value of the object is false. Otherwise, its value is true (even when the argument is the string "false")!