Skip to content

ES6 - Boolean

In JavaScript, a boolean represents one of two values: true or false. Booleans are fundamental for conditional logic and control flow.

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:

  • false
  • 0 (zero)
  • -0 (negative zero)
  • 0n (BigInt zero)
  • "" (empty string)
  • null
  • undefined
  • NaN (Not-a-Number)

All other values are considered “truthy”, including empty arrays [], empty objects {}, and non-empty strings like 'false'.

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()); // false

Instead of new Boolean(), use the Boolean() function as a converter to get a boolean primitive:

const primitiveTrue = Boolean('hello'); // true
const primitiveFalse = Boolean(0); // false
console.log(typeof primitiveTrue); // "boolean"

Instances of Boolean objects (and by extension, boolean primitives through auto-boxing) inherit properties from Boolean.prototype.

PropertyDescription
constructorReturns a reference to the Boolean function that created the instance’s prototype. (Inherited from Object.prototype)
prototypeThe Boolean.prototype property allows you to add properties and methods to all instances of Boolean objects (rarely modified directly).

Boolean primitives and Boolean objects have access to the following methods:

MethodDescription
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()); // false
  • Always prefer using boolean primitives (true, false).
  • Understand truthy and falsy values for conditional evaluations.
  • Avoid using new Boolean() constructor to prevent confusion. Use Boolean() as a function for type conversion if needed.