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
forwhen you know the iteration count. - Use
for...ofwhen you need values from an iterable. - Use
whilewhen 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
- Write one branch chain that logs different messages.
- Convert one loop into a different loop form.
- Note one condition that should be extracted into a named boolean.