Skip to content

Chapter 5 of 33

Control Flow

Design clear decisions with if, else, switch, truthiness, and guard clauses.

28 minutes 10 quick checksBy Subha Prasad
Lesson 5 of 33Course navigation

Lesson content

Read, practise, then check your understanding

if branches on truthiness. An else-if chain tests in order and runs the first matching branch. Guard clauses keep invalid paths short.

Explicit decisions

function classify(score) {
  if (!Number.isFinite(score) || score < 0 || score > 100) return "invalid";
  if (score >= 80) return "excellent";
  if (score >= 50) return "pass";
  return "retry";
}

switch matches cases using strict comparison. End cases with break, return, or throw unless fallthrough is intentional and documented. Empty arrays and objects are truthy; zero, NaN, empty string, null, undefined, and false are falsy. Use lookup maps or polymorphic handlers when a large switch becomes hard to extend.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes if statement?
Which JavaScript term matches this description: Conditional execution based on truthiness.
Which statement best describes else-if chain?
Which JavaScript term matches this description: Ordered branches where the first truthy condition wins.
Which statement best describes switch?
Which JavaScript term matches this description: Strict-equality matching of one discriminant against case expressions.
Which statement best describes fallthrough?
Which JavaScript term matches this description: Continuing into a following switch case when control is not terminated.
Which statement best describes guard clause?
Which JavaScript term matches this description: An early exit handling invalid or exceptional input.

0 of 10 checks passed

Your progress is saved on this device.