JavaScript Control Flow (if, else, switch) Quiz

JavaScript
0 Passed
0% acceptance

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.

50 Questions
~100 minutes
1

Question 1

What does the following conditional log with a non-empty string?

javascript
const status = 'active'
if (status) console.log('ok')
else console.log('fail')
A
ok
B
fail
C
undefined
D
Throws ReferenceError
2

Question 2

What value is logged when checking a falsy number in this conditional?

javascript
const n = 0
if (n) console.log('yes')
else console.log('no')
A
no
B
yes
C
0
D
undefined
3

Question 3

How many times does the inner branch execute in this nested if?

javascript
let x = 2
if (x > 1) {
  if (x < 3) console.log('hit')
}
A
1
B
0
C
2
D
Depends on the engine
4

Question 4

What is printed when using loose equality with null in this check?

javascript
const value = undefined
if (value == null) console.log('equal')
else console.log('different')
A
equal
B
different
C
undefined
D
Throws ReferenceError
5

Question 5

What result is logged using strict equality in this comparison?

javascript
const v = 0
if (v === false) console.log('same')
else console.log('diff')
A
diff
B
same
C
0
D
false
6

Question 6

How does this else-if chain resolve for code equal to 3?

javascript
const code = 3
if (code === 1) console.log('A')
else if (code === 3) console.log('B')
else console.log('C')
A
B
B
A
C
C
D
Nothing is logged
7

Question 7

Which branch executes when assignment is used inside the condition?

javascript
let flag = false
if (flag = true) console.log('run')
else console.log('skip')
A
run
B
skip
C
undefined
D
Throws SyntaxError
8

Question 8

What output occurs when switching on this string role?

javascript
const role = 'admin'
switch (role) {
  case 'user':
    console.log(1)
    break
  case 'admin':
    console.log(2)
    break
  default:
    console.log(3)
}
A
2
B
1
C
3
D
1 2
9

Question 9

What happens with fallthrough when break is omitted in this switch?

javascript
const x = 1
switch (x) {
  case 1:
    console.log('A')
  case 2:
    console.log('B')
}
A
A B
B
A
C
B
D
Nothing is logged
10

Question 10

Which output is produced when the default label appears before matching cases?

javascript
const t = 2
switch (t) {
  default:
    console.log('X')
  case 1:
    console.log('A')
  case 2:
    console.log('B')
}
A
X A B
B
B
C
A B
D
X B
11

Question 11

What value is logged using a ternary operator for a truthy score?

javascript
const score = 10
const label = score > 0 ? 'positive' : 'non-positive'
console.log(label)
A
positive
B
non-positive
C
undefined
D
true
12

Question 12

What is logged when a falsy empty string is checked?

javascript
const name = ''
if (!name) console.log('missing')
else console.log('present')
A
missing
B
present
C
undefined
D
Throws TypeError
13

Question 13

How many times is the console.log executed in this chained else-if structure?

javascript
const value = 5
if (value < 0) console.log('neg')
else if (value < 10) console.log('small')
else console.log('big')
A
1
B
0
C
2
D
3
14

Question 14

Which string is logged when short-circuiting with &&?

javascript
const ready = true
const msg = ready && 'go'
console.log(msg)
A
go
B
true
C
undefined
D
false
15

Question 15

What value is logged when the first operand to && is falsy?

javascript
const ready = 0
const msg = ready && 'go'
console.log(msg)
A
0
B
go
C
undefined
D
false
16

Question 16

What is logged when using || to provide a fallback for an empty array length check?

javascript
const items = []
const count = items.length || 10
console.log(count)
A
10
B
0
C
undefined
D
Throws TypeError
17

Question 17

What value is logged when using nullish coalescing with a zero value?

javascript
const retries = 0
const value = retries ?? 3
console.log(value)
A
0
B
3
C
undefined
D
null
18

Question 18

What string is logged when an if condition uses a non-empty array?

javascript
const list = []
if (list) console.log('truthy')
else console.log('falsy')
A
truthy
B
falsy
C
undefined
D
Throws TypeError
19

Question 19

How many times does the inner console.log execute in this nested if when value is 10?

javascript
const value = 10
if (value >= 0) {
  if (value % 2 === 0) console.log('even non-negative')
}
A
1
B
0
C
2
D
Depends on strict mode
20

Question 20

What is logged when break is used only in the middle switch case?

javascript
const level = 1
switch (level) {
  case 0:
    console.log('zero')
  case 1:
    console.log('one')
    break
  case 2:
    console.log('two')
}
A
one
B
zero one
C
one two
D
zero one two
21

Question 21

What does the switch statement log with a number that matches no case?

javascript
const mode = 3
switch (mode) {
  case 1:
    console.log('one')
    break
  case 2:
    console.log('two')
    break
  default:
    console.log('other')
}
A
other
B
one
C
two
D
Nothing is logged
22

Question 22

What does this chained ternary expression log for score equal to 75?

javascript
const score = 75
const label = score >= 90 ? 'A' : score >= 70 ? 'B' : 'C'
console.log(label)
A
B
B
A
C
C
D
undefined
23

Question 23

What is logged when using a guard with && to call a function only if it exists?

javascript
const logger = null
logger && logger('hello')
console.log('done')
A
done
B
hello
C
hello done
D
Throws TypeError
24

Question 24

What is printed when both parts of an || expression are truthy?

javascript
const left = 'L'
const right = 'R'
const result = left || right
console.log(result)
A
L
B
R
C
true
D
L R
25

Question 25

What does this condition log when checking an object directly?

javascript
const config = {}
if (config) console.log('configured')
else console.log('missing')
A
configured
B
missing
C
undefined
D
Throws TypeError
26

Question 26

What string is logged when combining || with && in this expression?

javascript
const a = 0
const b = 'X'
const c = 'Y'
const result = a || (b && c)
console.log(result)
A
Y
B
X
C
0
D
undefined
27

Question 27

What is logged when combining if and return inside a function?

javascript
function check(x) {
  if (x > 10) return 'high'
  return 'low'
}
console.log(check(5))
A
low
B
high
C
undefined
D
Throws TypeError
28

Question 28

What does the switch statement log when multiple cases share the same body?

javascript
const ch = 'B'
switch (ch) {
  case 'A':
  case 'B':
    console.log('letter')
    break
  default:
    console.log('other')
}
A
letter
B
other
C
A B
D
Nothing is logged
29

Question 29

How many lines are logged when break is omitted after a matching case that precedes default?

javascript
const val = 1
switch (val) {
  case 1:
    console.log('one')
  default:
    console.log('default')
}
A
2
B
1
C
0
D
3
30

Question 30

What does this function log when called with undefined?

javascript
function greet(name) {
  if (!name) console.log('Guest')
  else console.log('Hello ' + name)
}

greet(undefined)
A
Guest
B
Hello undefined
C
Hello
D
Nothing is logged
31

Question 31

Why is it generally safer to use strict equality (===) instead of loose equality (==) in control-flow conditions?

A
Strict equality avoids implicit type coercion, making branch conditions more predictable.
B
Strict equality is always faster by specification than loose equality.
C
Strict equality only works inside if statements, not in expressions.
D
Loose equality is deprecated and throws in modern engines.
32

Question 32

Why can relying on truthy checks like if (value) be dangerous when value can legitimately be 0?

A
0 is falsy, so the check will treat a valid numeric value as missing and skip the branch.
B
0 is truthy only inside loops and falsy in if statements.
C
JavaScript automatically converts 0 to null in conditions.
D
The engine throws if numbers are used directly in conditions.
33

Question 33

Why is placing the most specific conditions first in an if/else-if chain often a good practice?

A
Specific conditions might be subsets of broader ones; placing them first prevents them from being shadowed.
B
JavaScript requires conditions to be sorted alphabetically.
C
Else-if only works when conditions are ordered from smallest to largest numerically.
D
The interpreter optimises chains only when the first condition is the simplest.
34

Question 34

Why can deeply nested if/else blocks hinder maintainability compared to flatter control-flow structures?

A
Nested branches increase cognitive load and make it harder to follow all possible execution paths.
B
Deep nesting disables break and continue in loops.
C
The JavaScript engine refuses to execute more than three nested if statements.
D
Nested blocks cannot contain function calls.
35

Question 35

Why might you refactor a long switch statement into a lookup object or map?

A
Data-driven mappings are easier to extend, test, and share than long imperative switch blocks.
B
Switch statements are forbidden in strict mode.
C
Objects cannot be used inside control flow.
D
Maps automatically deep clone the control-flow logic.
36

Question 36

Why is it important to include a default branch in switch statements that act as decision routers?

A
A default branch ensures that unexpected values still lead to a defined behaviour or error path.
B
Without default, switch statements are not executed at all.
C
Default is required by the ECMAScript specification in all switch statements.
D
Debuggers only work when default is present.
37

Question 37

Why can using else for error handling after many successful if checks make debugging harder?

A
A final else may catch multiple unrelated failure cases, making it unclear which condition failed.
B
Else blocks cannot contain throw statements.
C
Engines skip else blocks in debugging mode.
D
Else blocks always rethrow errors automatically.
38

Question 38

Why might you prefer a ternary operator for very simple assignment branches?

A
Ternaries keep concise assignments in a single expression, improving readability when used sparingly.
B
If statements are deprecated in modern JavaScript.
C
Ternaries execute faster than if in all cases.
D
Ternaries automatically deep clone the chosen value.
39

Question 39

Why is it a bad idea to depend on loose equality checks like value == true in control flow?

A
Many values such as 1 or "1" can loosely equal true, leading to ambiguous and unsafe branching.
B
Boolean comparisons are not allowed inside if conditions.
C
Loose equality always returns false for booleans.
D
The engine warns and stops execution on loose boolean checks.
40

Question 40

Why is short-circuit evaluation useful when accessing nested properties on possibly null objects?

A
Using && or optional chaining can prevent TypeError by skipping property access when a parent is null or undefined.
B
Short-circuit evaluation automatically converts null into an empty object.
C
Short-circuiting forces all nested accesses to run regardless of conditions.
D
It disables throwing of errors in strict mode entirely.
41

Question 41

Why can mixing assignment and comparison operators in the same conditional expression be problematic?

A
It becomes easy to accidentally assign instead of compare, leading to unexpected branch execution.
B
JavaScript forbids comparisons after assignments.
C
Conditions are always evaluated right to left, ignoring assignments.
D
Engines automatically convert assignments to comparisons.
42

Question 42

Why might you prefer early returns over a single large if/else block wrapping an entire function body?

A
Early returns allow you to exit on invalid states quickly, flattening the remaining logic and improving readability.
B
Functions cannot contain more than one else by specification.
C
Return statements are required to appear before any if statement.
D
If a function ends with an else, it cannot be optimised.
43

Question 43

Why is it risky to depend on the default branch in a switch to handle both errors and valid but rare cases?

A
Mixing error handling and valid behaviour in default obscures the distinction and complicates debugging.
B
Default branches are executed only in development builds.
C
Default branches cannot throw errors.
D
Rare cases automatically skip the default branch.
44

Question 44

Why can overusing nested ternary operators harm readability even if they are technically correct?

A
Multiple nested ternaries create dense expressions that are difficult to scan and reason about.
B
Nested ternaries are executed in random order.
C
JavaScript forbids more than one ternary per function.
D
Ternaries cannot be nested inside return statements.
45

Question 45

Why is checking typeof value === "boolean" sometimes preferable before branching on value?

A
It ensures that the condition is a real boolean, avoiding coercion of unexpected types like numbers or strings.
B
The typeof operator always returns boolean for any primitive.
C
If you do not check typeof, if statements throw automatically.
D
Booleans cannot be used directly in if conditions without type checks.
46

Question 46

Why is it helpful to design control flow so that every possible input path is covered explicitly?

A
Explicit handling reduces the chance of silent failures and makes it clear what the system does for each case.
B
JavaScript throws when any path is not represented in an if statement.
C
Switch statements automatically fill missing branches at runtime.
D
Engines ignore branches that reference unexpected values.
47

Question 47

Why can ordering conditions based on likelihood (most common first) be beneficial in hot code paths?

A
Checking the most probable cases first can prevent unnecessary evaluations and slightly improve performance.
B
Engines require numeric conditions to be sorted ascending.
C
If branches are not sorted by probability, they are executed in random order.
D
JavaScript cannot short-circuit boolean expressions.
48

Question 48

Why is it better to handle unexpected values with explicit errors instead of silently continuing in complex control flow?

A
Failing fast with a clear error makes diagnosing issues easier than allowing the program to proceed with invalid assumptions.
B
JavaScript requires throw statements at the end of every function.
C
Silent continuation is not permitted in switch statements.
D
If you do not throw, unused branches are removed at runtime.
49

Question 49

Why can mixing roles of control flow and data transformation in the same large function be problematic?

A
Combining many responsibilities makes the branching logic harder to test and reason about.
B
Functions with control flow are not allowed to return values.
C
Data transformation always requires recursion instead of if statements.
D
Engines optimise only functions that contain control flow exclusively.
50

Question 50

Why should you document non-obvious control-flow decisions in code that involve business rules?

A
Comments or clear naming explain why certain branches exist, helping future maintainers avoid breaking important rules.
B
Engines require comments to execute if/else blocks correctly.
C
Undocumented branches are automatically removed during minification.
D
Business rules cannot be expressed through code alone.

QUIZZES IN JavaScript