Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A Boolean in JavaScript is a primitive logical data type that represents one of two binary states: true or false. It serves as the foundational mechanism for evaluating logical expressions and representing binary conditions in memory.

Instantiation and Syntax

Booleans can be instantiated using literal notation or the global Boolean() function.
// Literal assignment (Primitive)
const isActive = true;
const isComplete = false;

// Type casting using the Boolean() function (Primitive)
const hasValue = Boolean("Hello"); // true

Primitive vs. Wrapper Object

JavaScript distinguishes between the boolean primitive type and the Boolean object wrapper. Using the new keyword invokes the constructor and creates an object reference rather than a primitive value. This alters strict equality evaluation and memory allocation.
const primitiveBool = true;
const objectBool = new Boolean(true);

console.log(typeof primitiveBool); // "boolean"
console.log(typeof objectBool);    // "object"

console.log(primitiveBool === objectBool); // false
Note: Instantiating Booleans via the new Boolean() constructor is considered an anti-pattern. Because objects are inherently truthy, new Boolean(false) evaluates to true in a boolean context.

Type Coercion and Truthiness

When a non-boolean value is evaluated in a boolean context, JavaScript performs implicit type coercion. Values are categorized strictly as either “truthy” or “falsy”. There are exactly eight falsy values in JavaScript. If a value is not on this list, it evaluates to true.
// The 8 Falsy Values
Boolean(false);
Boolean(0);          // Number zero
Boolean(-0);         // Negative zero
Boolean(0n);         // BigInt zero
Boolean("");         // Empty string
Boolean(null);       // Absence of any object value
Boolean(undefined);  // Uninitialized variable
Boolean(NaN);        // Not a Number
All other values, including empty arrays, empty objects, and the string "false", are truthy.
// Truthy Examples
Boolean("false"); // true (non-empty string)
Boolean([]);      // true (object)
Boolean({});      // true (object)

Logical Operators and Boolean Casting

JavaScript logical operators (&&, ||, !) interact directly with boolean logic. While && and || return the value of one of their operands based on short-circuit evaluation, the logical NOT operator (!) always coerces its operand to a boolean primitive and inverts it. Applying the logical NOT operator twice (!!) is a standard syntactic shorthand for casting a value to a boolean primitive, functionally identical to calling Boolean().
// Logical NOT coerces to boolean and inverts
const inverted = !0; // true

// Double NOT casts to a boolean primitive
const castedString = !!"text"; // true
const castedNull = !!null;     // false
Master JavaScript with Deep Grasping Methodology!Learn More