Skip to Content
JavaScript Enlightenment
book

JavaScript Enlightenment

by Cody Lindley
December 2012
Beginner
164 pages
3h 15m
English
O'Reilly Media, Inc.
Content preview from JavaScript Enlightenment

Chapter 12. Boolean()

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>

Boolean() Parameters

The Boolean() constructor function takes one parameter to be converted to a boolean value (i.e., true or false). Any valid JavaScript value that is not 0, −0, null, false, NaN, undefined, or an empty string(""), will be converted to true. Below, we create two boolean object values. One true, one false.

Live Code

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

// parameter passed to Boolean() = 0 = false, thus foo = false
var foo = new Boolean(0)
console.log(foo);

// parameter passed to Boolean() = Math ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript: Best Practice

JavaScript: Best Practice

James Kolce, Moritz Kroger, Ivan Curic, Samier Saeed, Jeff Mott, M. David Green, Craig Buckler
CSS Secrets

CSS Secrets

Lea Verou
DOM Enlightenment

DOM Enlightenment

Cody Lindley
Deno Web Development

Deno Web Development

Alexandre Portela dos Santos

Publisher Resources

ISBN: 9781449344412Errata Page