Node.js Control

Control flow decides what runs next. Node uses the same JavaScript branching and looping rules you would use in any modern JS codebase.

Branching

if (status === "ready") {
  console.log("start");
} else if (status === "wait") {
  console.log("pause");
} else {
  console.log("stop");
}

Loops

  • Use for when you know the iteration count.
  • Use for...of when you need values from an iterable.
  • Use while when a condition controls the loop.

Switch logic

Switch statements are useful when one value selects one of several stable branches. Keep the cases short and explicit.

Practice

  1. Write one branch chain that logs different messages.
  2. Convert one loop into a different loop form.
  3. Note one condition that should be extracted into a named boolean.