Boolean()构造函数可用于创建布尔对象以及布尔原始值,表示true或false值。
在下面的代码中,我详细介绍了JavaScript中布尔值的创建。
示例:sample52.html
cript;toolbal:false;"><!DOCTYPEhtml><htmllang="en"><body><script>//CreateaBooleanobjectusingthenewkeywordandtheBoolean()constructor.varmyBoolean1=newBoolean(false);//Usingnewkeyword.console.log(typeofmyBoolean1);//Logs'object'.//CreateaBooleanliteral/primitivebydirectlyusingthenumberconstructorwithoutnew.varmyBoolean2=Boolean(0);//Withoutnewkeyword.console.log(typeofmyBoolean2);//Logs'boolean'.//CreateBooleanliteral/primitive(constructorleveragedbehindthescenes).varmyBoolean3=false;console.log(typeofmyBoolean3);//Logs'boolean'.console.log(myBoolean1,myBoolean2,myBoolean3);//Logsfalsefalsefalse.</script></body></html>