The Boolean type is the reference type corresponding to the boolean values.
To create a Boolean object, use the Boolean constructor and pass in either true or false:
var booleanObject = new Boolean(true);
Instances of Boolean type override the valueOf() method to return a primitive value of either true or false.
The toString() method is overridden to return a string of "true" or "false" when called.
It is confusing when using Boolean objects in Boolean expressions:
var falseObject = new Boolean(false); var result = falseObject && true; console.log(result); //true var falseValue = false; result = falseValue && true; console.log(result); //false
In this code, a Boolean object is created with a value of false.
That same object is used in AND operator with the primitive value true.
In Boolean math, false AND true is equal to false.
In the code it is the object named falseObject being evaluated, not its value ( false).
All objects are automatically converted to true in Boolean expressions, so falseObject actually is given a value of true in the expression.
Then, true AND true is equal to true.
The typeof operator returns "boolean" for the primitive but "object" for the reference.
A Boolean object is an instance of the Boolean type and will return true when used with the instanceof operator, whereas a primitive value returns false:
console.log(typeof falseObject); //object console.log(typeof falseValue); //boolean console.log(falseObject instanceof Boolean); //true console.log(falseValue instanceof Boolean); //false