2.1 if / else & switch
Control flow structures govern the branching path of execution based on conditional expressions. In JavaScript, conditionals evaluate the truthiness of expressions.
Figure 2.1: Decision flow of an if/else branch evaluating boolean truthiness in JavaScript.
const userRole = "admin";
switch (userRole) {
case "admin":
console.log("Full system root access granted");
break; // Break prevents unintended fall-through!
case "editor":
console.log("Edit access granted");
break;
default:
console.log("Standard view access");
}
2.2 Loops (for, while, for...of, for...in)
Loops automate repeated execution. Modern JavaScript offers specialized iteration statements depending on whether you are working with indices, collection values, or object keys.
Figure 2.2: Loop mechanics comparison. Use for...of to inspect elements/values of collections, and for...in to inspect object property keys.
2.3 Hoisting
Hoisting is JavaScript's default behavior of scanning declarations during the creation phase of an Execution Context and registering variable and function identifiers in memory before any code actually runs.
Figure 2.3: Hoisting lifts variable declarations to memory before code execution. var is initialized to undefined, while let/const reside in the Temporal Dead Zone.