Example of the initial value of a Boolean object in js

  • 2020-03-30 02:13:17
  • OfStack

A Boolean object is used to convert a nonlogical value to a logical value (true or false).
Creating a Boolean object

Use the keyword new to define a Boolean object. The following code defines a logical object called myBoolean:

Var myBoolean = new Boolean ()

Comment: the value of a logical object is false if it has no initial value or if its value is 0, -0, null, "", false, undefined, or NaN. Otherwise, the value is true (even when the argument is the string "false")!

All of the following lines of code create a Boolean object with an initial value of false.
 
<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> 

Operation results:

false
false
false
false
false
false

All the following lines of code create a Boolean object with an initial value of true:
 
<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> 

Operation results:

True,
True,
True,
True,
True,
About this initial value and Java and c are not the same, later write foreground attention!

Related articles: