Everything, Everywhere
Verified Specification | Standardized Formulas | Instant Precision
Secure & Private (Zero Data Retention) Free Access • No Sign-Up

Booleans & Comparisons

Booleans have only two possible values: true and false. They are essential for making decisions in your code.

Try It Yourself

      
📋 Copy JavaScript Booleans & Coalescing Cheat Sheet
// Truthiness & Falsy Values
// Falsy: false, 0, -0, 0n, "", null, undefined, NaN

// Nullish Coalescing Operator (??) vs Logical OR (||)
const count = 0;
const safeCount = count ?? 10;   // 0 (preserves 0 and "")
const wrongCount = count || 10;  // 10 (treats 0 as falsy!)

// Explicit Boolean Conversion
const isReady = Boolean(userToken);
const hasItems = !!items.length;

⚠️ 5 Fatal Traps & JavaScript Pitfalls

Trap #1: Logical OR (||) Overwriting Legitimate Falsy Values
Using val || fallback treats 0, false, and "" as missing values and replaces them with the fallback. Use the nullish coalescing operator val ?? fallback instead.
Trap #2: Empty Arrays and Objects Evaluating to Truthy
In JavaScript, Boolean([]) and Boolean({}) are both true! Checking if (items) does not test whether an array has elements; check items.length > 0.
Trap #3: Loose Equality (==) Type Coercion Hazards
The loose equality operator performs bizarre type coercions: "" == 0 is true, 0 == "0" is true, but "" == "0" is false! Always enforce strict equality ===.
Trap #4: new Boolean(false) Object Wrapper Trap
Invoking new Boolean(false) creates an object wrapper. Because all objects are truthy, if (new Boolean(false)) executes the branch! Use Boolean(val) without new.
Trap #5: Short-Circuiting in React/JSX Rendering 0
Writing count && <Badge /> in JSX renders 0 directly onto the webpage when count is 0. Use count > 0 && <Badge /> or a ternary.

💬 Frequently Asked Questions

What is the double exclamation mark (!!) idiom?
!! is a shorthand that casts any value to its boolean primitive equivalent (equivalent to Boolean(val)). The first ! negates and coerces, and the second ! restores correct polarity.
What values are strictly falsy in JavaScript?
There are exactly 8 falsy values: false, 0, -0, 0n (BigInt zero), "" (empty string), null, undefined, and NaN.
How does the nullish coalescing operator (??) work?
a ?? b evaluates and returns a unless a is strictly null or undefined, in which case it returns b, safely preserving 0 and false.
What is short-circuit evaluation?
In logical operations, if the outcome is guaranteed by the first operand, subsequent operands are not evaluated (e.g. false && expensiveFunc() skips the function).
Why should strict equality (===) be used everywhere?
Strict equality verifies both value and type without performing implicit type coercion, preventing unpredictable runtime bugs and logic failures.
Sponsored Utility
While You're Here
Sponsored Recommendations
Advertisement