Boolean (logical) objects are used to convert non-logical values into logical values (true or false).
Create Boolean object
Use the keyword new to define a Boolean object. The following code defines a logical object named myBoolean:
var myBoolean=new Boolean()
Note: If a logical object has no initial value or its value is 0, -0, null, "", false, undefined, or NaN, then the value of the object is false. Otherwise, its value is true (even when the argument is the string "false")!
All of the following lines of code create Boolean objects with an initial value of false.
Copy the code code as follows:
<script type="text/javascript">
var myBoolean=new Boolean();
document.write(myBoolean);
document.write("<br />");
var myBoolean=new Boolean(0);
document.write(myBoolean);
document.write("<br />");
var myBoolean=new Boolean(null);
document.write(myBoolean);
document.write("<br />");
var myBoolean=new Boolean("");
document.write(myBoolean);
document.write("<br />");
var myBoolean=new Boolean(false);
document.write(myBoolean);
document.write("<br />");
var myBoolean=new Boolean(NaN);
document.write(myBoolean);
document.write("<br />");
</script>
Running results:
false
false
false
false
false
false
All of the following lines of code create Boolean objects with an initial value of true:
Copy the code code as follows:
<script type="text/javascript">
var myBoolean=new Boolean(1);
document.write(myBoolean);
document.write("<br />");
var myBoolean=new Boolean(true);
document.write(myBoolean);
document.write("<br />");
var myBoolean=new Boolean("true");
document.write(myBoolean);
document.write("<br />");
var myBoolean=new Boolean("false");
document.write(myBoolean);
document.write("<br />");
var myBoolean=new Boolean("Bill Gates");
document.write(myBoolean);
document.write("<br />");
</script>
Running results:
true
true
true
true
true
Regarding this initial value, it is different from that of Java and C. Please pay attention when writing the front desk in the future!