🟨 JavaScript Intermediate

What is short-circuit evaluation in JavaScript?

Answer

Short-circuit evaluation means logical operators (&&, ||) stop evaluating as soon as the result is determined. AND (&&): if the left side is falsy, returns it immediately (does not evaluate right side). If truthy, returns the right side. false && sideEffect() — sideEffect never called. OR (||): if the left side is truthy, returns it immediately. If falsy, evaluates and returns the right side. true || sideEffect() — sideEffect never called. These are NOT just boolean operators — they return the actual values, not true/false. Practical patterns: user && user.name (safe access), count || 0 (default value), isValid && submit() (conditional execution), config.timeout || 3000 (fallback). Guard clauses: isLoggedIn && renderDashboard(). With ?? (nullish coalescing), short-circuiting only on null/undefined. Short-circuit also applies to ?? and optional chaining ?..