The Boolean() constructor creates Boolean objects. When called as a function, it returns primitive values of type Boolean.
const flag = new Boolean();
console.log(typeof flag);
// Expected output: object
console.log(flag === false);
// Expected output: false
const flag2 = Boolean();
console.log(typeof flag2);
// Expected output: boolean
console.log(flag2 === false);
// Expected output: truenew Boolean(value)
Boolean(value)Note: Boolean() can be called with or without new, but with different effects. See Return value.
valueThe initial value of the Boolean object.
When Boolean() is called as a function (without new), it returns value coerced to a boolean primitive.
When Boolean() is called as a constructor (with new), it coerces value to a boolean primitive and returns a wrapping Boolean object, which is not a primitive.
Warning: You should rarely find yourself using Boolean as a constructor.
The value passed as the first parameter is converted to a boolean value. If the value is omitted or is 0, -0, 0n, null, false, NaN, undefined, or the empty string (""), then the object has an initial value of false. All other values, including any object, an empty array ([]), or the string "false", create an object with an initial value of true.
Note: When the non-standard property document.all is used as an argument for this constructor, the result is a Boolean object with the value false. This property is legacy and non-standard and should not be used.
const bZero = new Boolean(0);
const bNull = new Boolean(null);
const bEmptyString = new Boolean("");
const bfalse = new Boolean(false);
typeof bfalse; // "object"
Boolean(bfalse); // trueNote how converting a Boolean object to a primitive with Boolean() always yields true, even if the object holds a value of false. You are therefore always advised to avoid constructing Boolean wrapper objects.
If you need to take the primitive value out from the wrapper object, instead of using the Boolean() function, use the object's valueOf() method instead.
const bfalse = new Boolean(false);
bfalse.valueOf(); // falseBoolean objects with an initial value of trueconst btrue = new Boolean(true);
const btrueString = new Boolean("true");
const bfalseString = new Boolean("false");
const bSuLin = new Boolean("Su Lin");
const bArrayProto = new Boolean([]);
const bObjProto = new Boolean({});