Conditionals
if/else & Decision Logic
Conditionals let your code make decisions. Based on whether a condition is truthy or falsy, different code paths execute - this is how programs respond dynamically to different inputs.
Conditionals are traffic lights for your code. Green (if) - proceed. Yellow (else if) - slow down and reconsider. Red (else) - take a different road entirely. Your program always picks exactly one road.
🔑 Key Concepts
Foundation of decision-making. Evaluates conditions in sequence; executes first truthy block.
One-line shorthand: condition ? valueIfTrue : valueIfFalse. Great for simple assignments.
switch for multi-value checks. && (AND), || (OR), ! (NOT), ?? (nullish coalescing).
💻 Code Examples
⚠️ Common Mistakes
Using == instead of === (type coercion surprises)
Always use === for comparisons. == does type coercion: 0 == false is true!
Forgetting break in switch cases
Without break, execution falls through to the next case. Always add break (or return).
if (x = 5) - accidental assignment
= is assignment, === is comparison. if (x === 5) is correct.
📋 Quick Reference
Live Code Playground
Edit the code and press ▶ Run to see output instantly.