Clean • Professional
A Boolean in JavaScript is a primitive data type that represents one of two possible values:Â true or false.
Booleans are mainly used in conditional statements, comparisons, and logical operations to control program flow.
They form the foundation of decision-making in JavaScript — for example, determining whether a condition is met, an element exists, or a comparison is valid.
typeof true → 'boolean'true and false (case-sensitive).if, else, while, for, and ternary (? :) expressions.5 > 3 automatically return Boolean values.You can directly assign true or false:
let isActive = true;
let isComplete = false;
Converts a value to a Boolean.
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean("Hello")); // true
console.log(Boolean("")); // false
This creates a Boolean object, not a primitive:
let boolObj = new Boolean(false);
console.log(typeof boolObj); // 'object'
console.log(boolObj); // [Boolean: false]
JavaScript automatically converts (or coerces) other types into Boolean values in logical contexts (like if statements).
Values are considered either truthy or falsy.
The following always evaluate to false:
false
0
-0
0n // BigInt zero
"" // empty string
null
undefined
NaN
Everything else is truthy — for example:
true
42
"hello"
[]
{}
function() {}
"0"
"false"
Example:
if ("") console.log("Truthy");
else console.log("Falsy"); // Output: Falsy
JavaScript provides logical operators that return Boolean values or expressions.
Returns true only if both operands are true.
console.log(true && true); // true
console.log(true && false); // false
In expressions, it returns the first falsy value or the last truthy one:
console.log("A" && "B"); // "B"
console.log("A" && ""); // ""
Returns true if at least one operand is true.
console.log(true || false); // true
console.log(false || false); // false
In expressions, returns the first truthy value:
console.log("" || "Default"); // "Default"
Inverts a Boolean value.
console.log(!true); // false
console.log(!0); // true
Converts any value into a strict Boolean.
console.log(!!"hello"); // true
console.log(!!0); // false
Booleans are often the result of comparison operators:
Strict equality (===) checks both value and type,
while loose equality (==) performs type coercion.
console.log(5 > 3); // true
console.log(5 == "5"); // true (loose equality)
console.log(5 === "5"); // false (strict equality)
console.log(Boolean(0)); // false
console.log(Boolean("text")); // true
console.log(Number(true)); // 1
console.log(Number(false)); // 0
console.log(String(true)); // "true"
console.log(String(false)); // "false"
console.log(Boolean([])); // true
console.log(Boolean({})); // true
console.log(Boolean(null)); // false
Booleans control logic flow in conditional constructs:
let isLoggedIn = true;
if (isLoggedIn) {
console.log("Welcome!");
} else {
console.log("Please log in.");
}
Using shorthand:
isLoggedIn && console.log("Welcome again!");
let a = false;
let b = new Boolean(false);
console.log(a == b); // true (loose)
console.log(a === b); // false (different types)
console.log(Boolean("false")); // true (non-empty string)
console.log(Boolean([])); // true (array is an object)
console.log(Boolean(null)); // false
!! for Safe Conversionlet val = "Hello";
console.log(!!val); // true
Example 1: Form Validation
let input = "";
if (!input) {
console.log("Please enter a value."); // Triggered because input is falsy
}
Example 2: Default Values with OR Operator
let username = "" || "Guest";
console.log(username); // "Guest"
Example 3: Short-Circuit Logic
let isAdmin = true;
isAdmin && console.log("Access granted");Â