JavaScript Control Flow (if, else, switch) Quiz
Explore 50 JavaScript control-flow questions covering branching, truthiness, equality pitfalls, nested decisions, switch mechanics, fallthrough, and short-circuit expressions. Twenty questions include runnable snippets so you can reason about exact evaluation before answering.
Question 1
What does the following conditional log with a non-empty string?
const status = 'active'
if (status) console.log('ok')
else console.log('fail')Question 2
What value is logged when checking a falsy number in this conditional?
const n = 0
if (n) console.log('yes')
else console.log('no')Question 3
How many times does the inner branch execute in this nested if?
let x = 2
if (x > 1) {
if (x < 3) console.log('hit')
}Question 4
What is printed when using loose equality with null in this check?
const value = undefined
if (value == null) console.log('equal')
else console.log('different')Question 5
What result is logged using strict equality in this comparison?
const v = 0
if (v === false) console.log('same')
else console.log('diff')Question 6
How does this else-if chain resolve for code equal to 3?
const code = 3
if (code === 1) console.log('A')
else if (code === 3) console.log('B')
else console.log('C')Question 7
Which branch executes when assignment is used inside the condition?
let flag = false
if (flag = true) console.log('run')
else console.log('skip')Question 8
What output occurs when switching on this string role?
const role = 'admin'
switch (role) {
case 'user':
console.log(1)
break
case 'admin':
console.log(2)
break
default:
console.log(3)
}Question 9
What happens with fallthrough when break is omitted in this switch?
const x = 1
switch (x) {
case 1:
console.log('A')
case 2:
console.log('B')
}Question 10
Which output is produced when the default label appears before matching cases?
const t = 2
switch (t) {
default:
console.log('X')
case 1:
console.log('A')
case 2:
console.log('B')
}Question 11
What value is logged using a ternary operator for a truthy score?
const score = 10
const label = score > 0 ? 'positive' : 'non-positive'
console.log(label)Question 12
What is logged when a falsy empty string is checked?
const name = ''
if (!name) console.log('missing')
else console.log('present')Question 13
How many times is the console.log executed in this chained else-if structure?
const value = 5
if (value < 0) console.log('neg')
else if (value < 10) console.log('small')
else console.log('big')Question 14
Which string is logged when short-circuiting with &&?
const ready = true
const msg = ready && 'go'
console.log(msg)Question 15
What value is logged when the first operand to && is falsy?
const ready = 0
const msg = ready && 'go'
console.log(msg)Question 16
What is logged when using || to provide a fallback for an empty array length check?
const items = []
const count = items.length || 10
console.log(count)Question 17
What value is logged when using nullish coalescing with a zero value?
const retries = 0
const value = retries ?? 3
console.log(value)Question 18
What string is logged when an if condition uses a non-empty array?
const list = []
if (list) console.log('truthy')
else console.log('falsy')Question 19
How many times does the inner console.log execute in this nested if when value is 10?
const value = 10
if (value >= 0) {
if (value % 2 === 0) console.log('even non-negative')
}Question 20
What is logged when break is used only in the middle switch case?
const level = 1
switch (level) {
case 0:
console.log('zero')
case 1:
console.log('one')
break
case 2:
console.log('two')
}Question 21
What does the switch statement log with a number that matches no case?
const mode = 3
switch (mode) {
case 1:
console.log('one')
break
case 2:
console.log('two')
break
default:
console.log('other')
}Question 22
What does this chained ternary expression log for score equal to 75?
const score = 75
const label = score >= 90 ? 'A' : score >= 70 ? 'B' : 'C'
console.log(label)Question 23
What is logged when using a guard with && to call a function only if it exists?
const logger = null
logger && logger('hello')
console.log('done')Question 24
What is printed when both parts of an || expression are truthy?
const left = 'L'
const right = 'R'
const result = left || right
console.log(result)Question 25
What does this condition log when checking an object directly?
const config = {}
if (config) console.log('configured')
else console.log('missing')Question 26
What string is logged when combining || with && in this expression?
const a = 0
const b = 'X'
const c = 'Y'
const result = a || (b && c)
console.log(result)Question 27
What is logged when combining if and return inside a function?
function check(x) {
if (x > 10) return 'high'
return 'low'
}
console.log(check(5))Question 28
What does the switch statement log when multiple cases share the same body?
const ch = 'B'
switch (ch) {
case 'A':
case 'B':
console.log('letter')
break
default:
console.log('other')
}Question 29
How many lines are logged when break is omitted after a matching case that precedes default?
const val = 1
switch (val) {
case 1:
console.log('one')
default:
console.log('default')
}Question 30
What does this function log when called with undefined?
function greet(name) {
if (!name) console.log('Guest')
else console.log('Hello ' + name)
}
greet(undefined)Question 31
Why is it generally safer to use strict equality (===) instead of loose equality (==) in control-flow conditions?
Question 32
Why can relying on truthy checks like if (value) be dangerous when value can legitimately be 0?
Question 33
Why is placing the most specific conditions first in an if/else-if chain often a good practice?
Question 34
Why can deeply nested if/else blocks hinder maintainability compared to flatter control-flow structures?
Question 35
Why might you refactor a long switch statement into a lookup object or map?
Question 36
Why is it important to include a default branch in switch statements that act as decision routers?
Question 37
Why can using else for error handling after many successful if checks make debugging harder?
Question 38
Why might you prefer a ternary operator for very simple assignment branches?
Question 39
Why is it a bad idea to depend on loose equality checks like value == true in control flow?
Question 40
Why is short-circuit evaluation useful when accessing nested properties on possibly null objects?
Question 41
Why can mixing assignment and comparison operators in the same conditional expression be problematic?
Question 42
Why might you prefer early returns over a single large if/else block wrapping an entire function body?
Question 43
Why is it risky to depend on the default branch in a switch to handle both errors and valid but rare cases?
Question 44
Why can overusing nested ternary operators harm readability even if they are technically correct?
Question 45
Why is checking typeof value === "boolean" sometimes preferable before branching on value?
Question 46
Why is it helpful to design control flow so that every possible input path is covered explicitly?
Question 47
Why can ordering conditions based on likelihood (most common first) be beneficial in hot code paths?
Question 48
Why is it better to handle unexpected values with explicit errors instead of silently continuing in complex control flow?
Question 49
Why can mixing roles of control flow and data transformation in the same large function be problematic?
Question 50
Why should you document non-obvious control-flow decisions in code that involve business rules?
