ES6 - Boolean
ES6 - Boolean Primitives and Object
Section titled “ES6 - Boolean Primitives and Object”In JavaScript, a boolean represents one of two values: true or false. Booleans are fundamental for conditional logic and control flow.
Boolean Primitives
Section titled “Boolean Primitives”Most of the time, you will work with boolean primitive values directly:
const isEnabled = true;const hasAccess = false;
if (isEnabled) { console.log('Feature is enabled.');} else { console.log('Feature is disabled.');}Values that are considered “falsy” in JavaScript (i.e., coerce to false in a boolean context) are:
false0(zero)-0(negative zero)0n(BigInt zero)""(empty string)nullundefinedNaN(Not-a-Number)
All other values are considered “truthy”, including empty arrays [], empty objects {}, and non-empty strings like 'false'.
The Boolean Object
Section titled “The Boolean Object”JavaScript also has a Boolean object, which is a wrapper around the boolean primitive. You can create a Boolean object using the new Boolean() constructor:
const boolObject = new Boolean(value);If value is omitted or is one of the falsy values listed above, the Boolean object’s internal value will be false. Otherwise, it will be true.
Important: It is generally discouraged to use the Boolean object constructor (new Boolean()). This is because it creates an object, and all objects in JavaScript are truthy, even if their wrapped value is false. This can lead to confusing behavior:
const falseObj = new Boolean(false); // Wrapped value is false
if (falseObj) { // This condition is TRUE because falseObj is an object! console.log('This will be logged, surprisingly!');}
// To get the primitive value:console.log(falseObj.valueOf()); // falseInstead of new Boolean(), use the Boolean() function as a converter to get a boolean primitive:
const primitiveTrue = Boolean('hello'); // trueconst primitiveFalse = Boolean(0); // false
console.log(typeof primitiveTrue); // "boolean"Boolean Object Properties
Section titled “Boolean Object Properties”Instances of Boolean objects (and by extension, boolean primitives through auto-boxing) inherit properties from Boolean.prototype.
| Property | Description |
|---|---|
constructor | Returns a reference to the Boolean function that created the instance’s prototype. (Inherited from Object.prototype) |
prototype | The Boolean.prototype property allows you to add properties and methods to all instances of Boolean objects (rarely modified directly). |
Boolean Object Methods
Section titled “Boolean Object Methods”Boolean primitives and Boolean objects have access to the following methods:
| Method | Description |
|---|---|
toString() | Returns a string representation of the boolean value, either “true” or “false”. |
valueOf() | Returns the primitive value of the Boolean object (true or false). |
const bTrue = true;const bFalseObj = new Boolean(false);
console.log(bTrue.toString()); // "true"console.log(bFalseObj.toString()); // "false"
console.log(bTrue.valueOf()); // true (already primitive)console.log(bFalseObj.valueOf()); // falseSummary:
Section titled “Summary:”- Always prefer using boolean primitives (
true,false). - Understand truthy and falsy values for conditional evaluations.
- Avoid using
new Boolean()constructor to prevent confusion. UseBoolean()as a function for type conversion if needed.