64x64 icon dark hosting
Deploy New Relic now and get $135 off your Tuts+ subscription.


This post is part of a series called JavaScript Succinctly.
Working With Primitive String, Number and Boolean Values

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 following code, I detail the creation of Boolean values in JavaScript.

Sample: sample52.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. In the following sample, we create two Boolean object values: One true and one false.

Sample: sample53.html

When used with the new keyword, instances from the Boolean() constructor produce an actual complex object. You should avoid creating Boolean values using the Boolean() constructor (instead, use literal/primitive numbers) due to the potential problems associated with the typeof operator. The typeof operator reports Boolean objects as 'object', instead of the primitive label ('boolean') you might expect. Additionally, the literal/primitive value is faster to write.

Boolean() Properties and Methods

The Boolean() object has the following properties:

Properties (e.g., Boolean.prototype;):

Boolean Object Instance Properties and Methods

Boolean object instances have the following properties and methods (not including inherited properties and methods):

Instance Properties (e.g., var myBoolean = false; myBoolean.constructor;):

Instance Methods (e.g., var myNumber = false; myBoolean.toString();):

Non-Primitive False Boolean Objects Convert to True

A false Boolean object (as opposed to a primitive value) created from the Boolean() constructor is an object, and objects convert to true. Thus, when creating a false Boolean object via the Boolean() constructor, the value itself converts to true. In the following sample, I demonstrate how a false Boolean object is always "truthy.”

Sample: sample54.html

If you need to convert a non-Boolean value into a Boolean, just use the Boolean() constructor without the new keyword and the value returned will be a primitive value instead of a Boolean object.

Certain Things Are False, Everything Else Is True

It has already been mentioned, but is worth mentioning again because it pertains to conversions: If a value is 0, -0, null, false, NaN, undefined, or an empty string(""), it is false. Any value in JavaScript except the aforementioned values will be converted to true if used in a Boolean context (i.e. if (true) {};).

Sample: sample55.html


It's critical that you understand which JavaScript values are reduced to false so you are aware that all other values are considered true.