Summary: in this tutorial, you will learn about the JavaScript Boolean
object and the differences between the Boolean
object and the boolean
primitive type.
JavaScript boolean primitive type
JavaScript provides a boolean primitive type that has two values of true
and false
. The following example declares two variables that hold boolean values of false
and true
:
let isPending = false;
let isDone = true;
Code language: JavaScript (javascript)
When you apply the typeof
operator to a variable that holds a primitive boolean value, you get the boolean
as the following example:
console.log(typeof(isPending)); // boolean
console.log(typeof(isDone)); // boolean
Code language: JavaScript (javascript)
JavaScript Boolean object
In addition to the boolean
primitive type, JavaScript also provides you with the global Boolean()
function, with the letter B
in uppercase, to cast a value of another type to boolean.
The following example shows how to use the Boolean()
function to convert a string into a boolean value. Because the string is not empty, it returns true
.
let a = Boolean('Hi');
console.log(a); // true
console.log(typeof(a)); // boolean
Code language: JavaScript (javascript)
The Boolean
is also a wrapper object of the boolean
primitive type. It means that when you pass either true
or false
to the Boolean
constructor, it’ll create a Boolean
object. For example:
let b = new Boolean(false);
Code language: JavaScript (javascript)
To get the primitive value back, you call the valueOf()
method of the Boolean
object as follows:
console.log(b.valueOf()); // false
Code language: JavaScript (javascript)
However, if you call the toString()
method of a Boolean
object, you get a string value "true"
or "false"
. For example:
console.log(b.toString()); // "false"
Code language: JavaScript (javascript)
JavaScript boolean vs. Boolean
Consider this example:
let completed = true;
let active = new Boolean(false);
Code language: JavaScript (javascript)
First, active
is an object so you can add a property to it:
active.primitiveValue = active.valueOf();
console.log(active.primitiveValue); // false
Code language: JavaScript (javascript)
However, you cannot do it with the primitive boolean variable like the completed
variable:
completed.name = 'primitive';
console.log(completed.name); // undefined
Code language: JavaScript (javascript)
Second, the typeof
of Boolean object returns object
, whereas the typeof
of a primitive boolean value returns boolean
.
console.log(typeof completed); // boolean
console.log(typeof active); // object
Code language: JavaScript (javascript)
Third, when applying the instanceof
operator to a Boolean
object, it returns true
. However, it returns false
if you apply the instanceof
operator to a boolean value.
console.log(completed instanceof Boolean); // false
console.log(active instanceof Boolean); // true
Code language: JavaScript (javascript)
It is a good practice never to use the Boolean
object because it will create much confusion, especially when using in an expression. For example:
let falseObj = new Boolean(false);
if (falseObj) {
console.log('weird part of the Boolean object');
}
Code language: JavaScript (javascript)
How the script works.
- First, create
falseObj
as aBoolean
object wrapper for thefalse
value. - Second, use
falseObj
in theif
statement. BecausefalseObj
is an object, and JavaScript engine coerces it to a boolean value oftrue
. As a result, the statement inside theif
block is executed.
The following table summarizes the differences between JavaScript Boolean
and boolean
:
Operator | boolean | Boolean |
---|---|---|
typeof | boolean | object |
instanceof Boolean | false | true |
It is recommended that you use the Boolean()
function to convert a value of a different type to a Boolean type, but you should never use the Boolean
as a wrapper object of a primitive boolean value.
In this tutorial, you have learned about the JavaScript Boolean
object and the differences between the Boolean
object and boolean
primitive type.