Episode 7 — DSA with JavaScript / 7.1 — Conditional Statements
7.1 — Quick Revision: Conditional Statements
Cheat sheet
if / if-else / if-else-if
if (condition) { ... }
if (condition) { ... } else { ... }
if (cond1) { ... }
else if (cond2) { ... }
else { ... }
if (condition) { ... }
else if (condition2) { ... }
else { ... }
switch
switch (expr) {
case val1: /* ... */ break;
case val2: /* ... */ break;
default: /* ... */
}
Ternary
const result = condition ? valueA : valueB;
Logical operators
a && b // AND — short-circuits on falsy
a || b // OR — short-circuits on truthy
a ?? b // Nullish coalescing — falls through only on null/undefined
!a // NOT
Falsy values (JS)
false | 0 | -0 | 0n | "" | null | undefined | NaN
Everything else is truthy (including [], {}, "0", "false").
== vs ===
| Expression | == | === |
|---|---|---|
0 == "" | true | false |
null == undefined | true | false |
1 == "1" | true | false |
NaN == NaN | false | false |
Rule: Always use === and !==.
Guard clause pattern
function process(data) {
if (!data) return; // guard
if (!data.valid) return; // guard
// happy path
}
Object lookup vs switch
// Switch
switch (key) {
case "a": return 1;
case "b": return 2;
default: return 0;
}
// Object lookup (cleaner for value mapping)
const map = { a: 1, b: 2 };
return map[key] ?? 0;
Common pitfalls
=vs==vs===— assignment vs loose vs strict- Missing
breakin switch — causes fall-through - Dangling else — binds to nearest if without braces
||vs??—||treats0and""as falsy- Nested ternaries — hard to read, use if-else instead
- Not handling
defaultin switch — always include it - Forgetting
NaN !== NaN— useNumber.isNaN() - Type coercion surprises —
[] == falseis true
Decision tree: which conditional to use?
Need to return a value from the condition?
├── YES → Is it a simple binary choice?
│ ├── YES → Ternary
│ └── NO → Object/Map lookup
└── NO → How many branches?
├── 1 → if
├── 2 → if-else
├── 3-4 → if-else-if
└── 5+ → switch or object lookup
Complexity note
All basic conditional operations are O(1) — they evaluate a fixed number of comparisons regardless of input size.
A chain of N conditions is O(N) in the worst case (all conditions checked).
A switch with N cases may be optimized to a jump table — O(1) in many compilers.
Self-check drill
SC-001
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-002
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-003
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-004
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-005
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-006
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-007
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-008
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-009
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-010
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-011
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-012
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-013
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-014
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-015
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-016
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-017
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-018
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-019
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-020
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-021
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-022
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-023
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-024
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-025
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-026
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-027
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-028
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-029
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-030
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-031
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-032
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-033
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-034
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-035
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-036
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-037
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-038
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-039
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-040
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-041
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-042
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-043
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-044
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-045
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-046
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-047
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-048
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-049
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-050
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-051
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-052
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-053
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-054
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-055
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-056
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-057
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-058
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-059
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-060
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-061
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-062
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-063
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-064
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-065
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-066
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-067
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-068
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-069
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-070
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-071
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-072
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-073
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-074
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-075
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-076
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-077
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-078
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-079
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-080
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-081
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-082
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-083
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-084
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-085
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-086
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-087
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-088
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-089
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-090
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-091
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-092
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-093
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-094
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-095
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-096
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-097
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-098
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-099
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-100
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-101
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-102
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-103
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-104
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-105
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-106
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-107
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-108
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-109
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-110
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-111
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-112
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-113
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-114
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-115
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-116
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-117
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-118
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-119
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-120
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-121
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-122
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-123
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-124
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-125
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-126
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-127
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-128
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-129
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-130
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-131
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-132
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-133
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-134
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-135
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-136
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-137
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-138
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-139
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-140
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-141
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-142
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-143
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-144
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-145
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-146
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-147
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-148
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-149
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-150
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-151
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-152
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-153
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-154
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-155
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-156
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-157
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-158
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-159
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-160
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-161
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-162
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-163
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-164
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-165
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-166
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-167
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-168
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-169
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-170
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-171
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-172
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-173
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-174
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-175
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-176
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-177
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-178
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-179
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-180
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-181
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-182
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-183
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-184
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-185
- Q: What is a guard clause?
- A: Early return for invalid/edge cases
SC-186
- Q: Difference between || and ???
- A: || returns right on ANY falsy; ?? only on null/undefined
SC-187
- Q: What is the dangling else problem?
- A: else binds to nearest if — use braces
SC-188
- Q: Why avoid nested ternaries?
- A: Hard to read — use if-else for complex logic
SC-189
- Q: How does fall-through work in switch?
- A: Without break, execution continues to next case
SC-190
- Q: What is object lookup pattern?
- A: Replace switch/if-else with key-value mapping
SC-191
- Q: What does !!value do?
- A: Converts to boolean: !!0 = false, !!'hi' = true
SC-192
- Q: Is [] truthy or falsy in JS?
- A: Truthy — empty array is truthy
SC-193
- Q: What does typeof null return?
- A: 'object' — a historical JS bug
SC-194
- Q: How to check NaN?
- A: Number.isNaN(x) — not x === NaN
SC-195
- Q: When to use try-catch vs if?
- A: try-catch for unexpected errors, if for expected conditions
SC-196
- Q: What are the 7 falsy values in JS?
- A: false, 0, -0, 0n, '', null, undefined, NaN
SC-197
- Q: Difference between == and ===?
- A: == coerces types, === does not
SC-198
- Q: When to use switch over if-else?
- A: Comparing one variable against many constants
SC-199
- Q: What is short-circuit evaluation?
- A: Stop evaluating when result is determined
SC-200
- Q: What is a guard clause?
- A: Early return for invalid/edge cases