Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

12. Boolean() > Conceptual Overview of Using the Boolean() Object

Conceptual Overview of Using the Boolean() Object

The Boolean() constructor function can be used to create boolean objects, as well as boolean primitive values, that represent either a true or a false value.

In the code below, I detail the creation of boolean values in JavaScript.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

// create boolean object using the new keyword and the Boolean() constructor
var myBoolean1 = new Boolean(false); 
// using new keyword
console.log(typeof myBoolean1); // logs 'object'

/* create boolean literal/primitive by directly using the number constructor 
without new */
var myBoolean2 = Boolean(0); // without new keyword
console.log(typeof myBoolean2); // logs 'boolean'

// create boolean literal/primitive (constructor leveraged behind the scene)
var myBoolean3 = false;
console.log(typeof myBoolean3); // logs 'boolean'
console.log(myBoolean1, myBoolean2, myBoolean3); // logs false false false

</script></body></html>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint