Control flows are logics that determines what piece of code runs, when it runs, and how often it runs based on conditions and repetitions. Control flows dictate the order in which a program's code is executed.
Control flows are divided into 2 categories; conditional statements and loops.
Conditional Statements
Conditional statements are control flows that allows you to execute blocks of code based on a condition. Examples of control flows under conditional statements are if, else, else if, switch, and ternary operator ? :.
-
if: Executes a block of code if the condition is true.let age = 18; if (age >= 18) { console.log("You are an adult."); } -
else: Executes a block of code if the condition is false.let age = 16; if (age >= 18) { console.log("You are an adult."); } else { console.log("You are a minor."); } -
else if: Tests another condition if the previous one is false.let age = 20; if (age < 18) { console.log("You are a minor."); } else if (age < 21) { console.log("You are a young adult."); } else { console.log("You are an adult."); } -
switch: A way to handle multiple conditions based on a single variable. Eachcaserepresents a possible value to match. Thebreakstatement at the end of each case tells JavaScript to stop executing and exit the switch block once a match is found. Withoutbreak, JavaScript will continue executing every case below the matching one — a behaviour called fall-through — which can produce unintended results.let day = 3; switch (day) { case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; case 3: console.log("Wednesday"); break; case 4: console.log("Thursday"); break; case 5: console.log("Friday"); break; case 6: console.log("Saturday"); break; case 7: console.log("Sunday"); break; default: console.log("Invalid day"); } -
Ternary Operator
? :: A compact, single-line alternative to anif...elsestatement. It takes a condition, and returns one value if the condition is true and another if it is false.Syntax:
condition ? valueIfTrue : valueIfFalselet age = 20; let status = age >= 18 ? "adult" : "minor"; console.log(status); // Output: "adult"This is equivalent to writing:
let status; if (age >= 18) { status = "adult"; } else { status = "minor"; }The ternary operator is best used for simple, short conditions. For more complex logic, a full
if...elseblock is more readable.
Loops
Loops are control flows that allow the repetition of a block of code for a certain number of times. Examples are for, while, do…while, for…of, for…in loop.
-
for: A loop that runs a block of code a specific number of times.for (let i = 0; i < 5; i++) { console.log(i); } // Prints numbers from 0 to 4 -
for…ofloop: Thefor...ofloop is used to iterate over iterable objects like arrays, strings, maps, sets, etc. It directly gives each element instead of using an index like theforloop.for (let variable of iterable) { // Code to execute for each element } -
for…inloop: This loops over the keys (also called properties) of an object. Not the values, but the keys of the object.for…inis mainly used for objects because JavaScript objects don't have indexes like arrays.const person = { name: "Ada", age: 20, country: "Nigeria" }; for (let key in person) { console.log(key, person[key]); } // Output: // name Ada // age 20 // country Nigeria -
while: A loop that runs as long as the condition is true.let i = 0; while (i < 5) { console.log(i); // Prints numbers from 0 to 4 i++; } -
do...while: Similar to thewhileloop, but it guarantees that the code runs at least once.let i = 0; do { console.log(i); // Prints numbers from 0 to 4 i++; // Increment i } while (i < 5);