Boolean Object
The Boolean object is a wrapper for boolean primitive values. It provides a way to create boolean objects, though they are rarely used because primitive booleans are more efficient. Any object, including a Boolean object, is truthy even if it wraps false.
Key Points
Section titled “Key Points”- Primitive booleans:
true,false. Booleanconstructor:new Boolean(value)returns an object.- Avoid using Boolean objects in conditionals because they are always truthy.
Code Example
Section titled “Code Example”const boolPrimitive = true;const boolObject = new Boolean(false);
console.log(typeof boolPrimitive); // "boolean"console.log(typeof boolObject); // "object"console.log(boolPrimitive === boolObject); // false
// Dangerous: boolObject is truthy!if (boolObject) { console.log("This will execute!"); // this runs}