Curriculum
Concept 17 // Module 02: Values, Operators & Control Flow
Truthy, Falsy & Short-Circuit Evaluation
Module 02: Values, Operators & Control Flow•Concept 17 of 100• 5 min read
Truthy, Falsy & Short-Circuit Evaluation
In JavaScript, any value evaluated in a boolean context is either **truthy** or **falsy**. Exactly eight values are falsy: `false`, `0`, `-0`, `0n`, `""`, `null`, `undefined`, and `NaN`. All other values, including empty arrays `[]` and empty objects `{}`, are truthy.
The Core Concept
Short-circuit evaluation is evaluated from left to right. Logical `&&` halts at the first falsy operand and returns it; logical `||` halts at the first truthy operand and returns it.
Syntax Reference
// Boolean context coercions
if (value) { /* runs if truthy */ }
Boolean(value); // explicit cast
!!value; // double negation idiom
// Short-circuiting operators
const a = x && y; // returns x if falsy, else y
const b = x || y; // returns x if truthy, else y
const c = x ?? y; // returns x unless null/undefinedLive Interactive Studio
Even empty structures `[]` and `{}` evaluate to `true` because they are references to heap objects.
JavaScript Editor
Loading...
Console Output
Waiting for output...
Key Code Patterns in Action
01
The 8 Falsy Values
const falsyList = [false, 0, -0, 0n, "", null, undefined, NaN];
falsyList.forEach(val => {
console.log(val, "->", Boolean(val)); // all false
});
// Watch out: Objects and arrays are ALWAYS truthy!
console.log(Boolean([])); // true
console.log(Boolean({})); // true
console.log(Boolean("0")); // trueEven empty structures `[]` and `{}` evaluate to `true` because they are references to heap objects.
02
Short-Circuiting and Nullish Coalescing
const userCount = 0;
// Buggy fallback with ||
const display1 = userCount || 10;
console.log(display1); // 10 ❌ (0 was treated as absent!)
// Fixed with ?? (Nullish Coalescing)
const display2 = userCount ?? 10;
console.log(display2); // 0 ✅ (only falls back on null/undefined)`??` only falls back when the left operand is `null` or `undefined`, preventing accidental overrides of `0` or `false`.
03
1. Core Usage: Truthy, Falsy & Short-Circuit Evaluation
// Core syntax and usage for Truthy, Falsy & Short-Circuit Evaluation
const feature = 'truthy-falsy-short-circuit-evaluation';
console.log('Concept 17:', feature);
console.log('Active:', true);Illustrates the fundamental invocation and syntax structure for Truthy, Falsy & Short-Circuit Evaluation.
04
2. Defensive Handling & Edge Case
// Edge-case defensive handling
function handleSafe(input) {
if (input === null || input === undefined) return 'safe-fallback';
return String(input).trim();
}
console.log(handleSafe('truthy-falsy-short-circuit-evaluation'));
console.log(handleSafe(null));Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Truthy, Falsy & Short-Circuit Evaluation.
05
3. Clean Architecture & Production Pattern
// Production-grade pattern for Truthy, Falsy & Short-Circuit Evaluation
const settings = Object.freeze({
id: 'truthy-falsy-short-circuit-evaluation',
ready: true,
timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);Demonstrates clean, immutable production code avoiding unintended state mutations.
Real-World Engineering Takeaways
Safe Default Fallbacks: Using `??` to provide config defaults without clobbering zero or false.
Conditional Execution: `isValid && submitForm()` for lightweight guard clauses.
UI Toggle States: Sanitizing dynamic props into pure booleans via `!!isExpanded`.
Under the Hood: Engine & Memory Mechanics
How JavaScript Executes This
V8 engine implements fast inline branching: for booleans and Smis, the condition register is inspected directly; for pointers, the engine checks for the special oddball heap constants (`oddball: false`, `oddball: null`).
Quick Knowledge Check
Test your understanding on these 10 questions (one by one):
Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model
What is the core purpose of Truthy, Falsy & Short-Circuit Evaluation in JavaScript?
// Concept 17: Truthy, Falsy & Short-Circuit Evaluation
// Understanding the core mental model1 / 10