JavaScript Course

Control Structures if, else, switch, loops

Control Flow: The Crossroads of Your Code

Welcome to the fascinating realm of Control Flow, where you'll learn to steer the course of your code like a pro. Think of your code as a journey, and Control Flow as the map that guides it, telling the program which path to take next.

Decision Making with if...else Statements

Imagine your code as a detective, faced with a series of clues. Using if statements, it can evaluate these clues and choose the right path forward. For instance, if (userAge >= 18) { allowAccess(); } checks if the user is old enough to access a certain page. But what if they're too young? That's where else comes in, directing the program to a different destination.

Multi-Branch Decisions with switch Statements

Now imagine you have a choice between multiple options, like flavors of ice cream. Instead of writing a long chain of if statements, you can use a switch statement to evaluate the user's choice and jump directly to the corresponding block of code. It's like a quick and easy way to navigate a menu of choices.

Stay tuned for Decision Making with if...else Statements, where we'll dive deeper into the world of decision-making code.

Decision Making with if...else Statements: The Art of Guiding Your Code

In the world of programming, decisions are crucial. That's where if...else statements come in. They allow your code to evaluate conditions and take different paths based on the outcome.

Imagine you're creating a game where players enter their age to determine if they can play. An if statement would look something like this:

if (playerAge >= 18) {
  console.log("You can play!");
} else {
  console.log("Sorry, you're too young to play.");

Here, if the player's age is 18 or older, they can play. Otherwise, they see a message saying they're too young.

If you have multiple options, like different levels in a game, you can use a switch statement:

switch (level) {
  case "easy":
    console.log("Easy mode selected.");
  case "medium":
    console.log("Medium mode selected.");
  case "hard":
    console.log("Hard mode selected.");
    console.log("Invalid level.");

This statement checks the value of the "level" variable and jumps to the corresponding block of code.

With these powerful tools, you can guide your code to make smart decisions and create dynamic and engaging programs. Next up, we'll explore Multi-Branch Decisions with switch Statements, where you'll master the art of handling multiple choices with ease...

Multi-Branch Decisions with switch Statements

Imagine you're at a restaurant and have a long list of delicious dishes to choose from. To make your decision, you can either go through the menu item by item or use a switch statement. A switch statement in programming is like a quick way to jump to the section of code that corresponds to your choice.

Let's say we have a website where users can choose their favorite color. We can use a switch statement to display the user's choice:

switch (color) {
  case "red":
    console.log("Your favorite color is red.");
  case "blue":
    console.log("Your favorite color is blue.");
  case "green":
    console.log("Your favorite color is green.");
    console.log("Invalid color.");

The switch statement evaluates the value of the "color" variable and jumps to the corresponding block of code. In this case, if the user's favorite color is "red," the program will print "Your favorite color is red."

Switch statements are a powerful tool for handling multiple choices in your code. They make your code more readable and easier to maintain.

Challenge: Try using a switch statement to create a program that calculates the shipping cost for different countries.

Next Up: Flow Control with Loops (for, while, do...while)

Flow Control with Loops (for, while, do...while)

Loops: The Repeaters of Code

Loops are your secret weapon for repeating blocks of code. They allow you to automate tasks that would otherwise require a lot of manual effort.

Types of Loops

There are three main types of loops in JavaScript:

  • for loops: Best for repeating a block of code a specific number of times.
  • while loops: Great for repeating code while a condition remains true.
  • do...while loops: Similar to while loops, but the block of code always runs at least once.

for Loops: Counting with Confidence

for loops use a counter variable to keep track of how many times the loop has run. They're perfect for tasks like iterating through arrays or objects.

For example, to print the numbers from 1 to 10, you could use a for loop like this:

for (let i = 1; i <= 10; i++) {

while Loops: The Conditional Repeaters

while loops repeat a block of code as long as a specified condition is true. They're often used for tasks like waiting for user input or checking for a certain value.

For instance, to keep asking the user for input until they enter a valid value, you could use a while loop like this:

while (input === "") {
  input = prompt("Please enter a valid value");

do...while Loops: The Guaranteed-Run Repeaters

do...while loops are similar to while loops, but the block of code is guaranteed to run at least once. They're ideal for situations where you need to perform an action at least once before checking the condition.

For example, to print the numbers from 1 to 10, even if the condition is false, you could use a do...while loop like this:

let i = 1;
do {
} while (i <= 10);

Real-World Examples of Loops

Loops are used extensively in programming. Here are some common examples:

  • Iterating through arrays to find the maximum value
  • Checking user input to ensure it's valid
  • Running a game until the player wins or loses
  • Printing patterns or generating random numbers

Remember: Loops are your friends when you need to repeat tasks. Use them wisely and you'll be writing more efficient and elegant code!

Next Up: Iteration with Arrays and Objects

Iteration with Arrays and Objects

Navigating Arrays with Loops

Imagine an array as a bookshelf filled with books. To access each book, you might use a loop to iterate through them, assigning each book to a variable for further processing. Arrays are commonly used to store lists of data, like names, scores, or even objects.

Looping Through Objects

Objects are like more complex bookshelves, with not just books but also drawers. To access the data inside, you might use a loop to navigate the object's properties, treating each property like a key that opens a drawer to reveal its contents. Objects are often used to store more structured data, like user profiles or product information.

Practical Tips for Iteration

Visualize Arrays and Objects: Think of arrays as lists and objects as bookshelves to make their structure easier to grasp.

Use the Right Loop: Choose the loop type (for, while, or do...while) that best matches your iteration needs.

Iterate with Confidence: Practice looping through arrays and objects to become comfortable with these fundamental coding techniques.

Suspenseful Question for Next Section:

What happens when you combine loops with conditional statements? Stay tuned for our next section, Conditional Loops and Early Exit, to find out how you can add logic and control to your iterations!

Conditional Loops and Early Exit

In the world of programming, you often encounter situations where you need to repeat a task multiple times, and based on certain conditions, you may want to exit the loop early. This is where conditional loops come into play.

Breaking Out of Loops with "break"

The break statement allows you to immediately exit a loop, no matter where you are in the loop body. It's like a quick escape hatch that lets you get out of the loop as soon as a certain condition is met.

For example, let's say you're iterating through an array of numbers and you want to stop when you find a number that's greater than 100. You can do this with a conditional loop like this:

// Array of numbers
const numbers = [10, 20, 30, 110, 50, 60];

// Loop through the array for (let i = 0; i < numbers.length; i++) { if (numbers[i] > 100) { // Exit the loop early break; } }

In this example, the loop will stop as soon as it encounters the number 110, which is the first number that's greater than 100.

Continuing Loops with "continue"

The continue statement is another useful tool for controlling loops. It allows you to skip the remaining statements in the current iteration of the loop and move on to the next iteration.

For instance, you might want to skip printing the numbers that are divisible by 3 when iterating through an array. You can do this with a conditional loop like this:

// Array of numbers
const numbers = [10, 20, 30, 110, 50, 60];

// Loop through the array for (let i = 0; i < numbers.length; i++) { // Skip numbers divisible by 3 if (numbers[i] % 3 === 0) { continue; }

// Print the number console.log(numbers[i]); }

In this example, the loop will skip printing the numbers 30, 60, and 110 because they are divisible by 3.

Nested Loops and Complex Flow

Loops can be nested within other loops, creating complex control flow. Imagine you have an array of arrays, and you want to iterate through each inner array and print its elements. You can use nested loops to achieve this:

// Array of arrays
const myArray = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];

// Loop through the outer array for (let i = 0; i < myArray.length; i++) { // Loop through the inner array for (let j = 0; j < myArray[i].length; j++) { // Print the element console.log(myArray[i][j]); } }

This code will print the elements in the following order:


Practical Tips

  • Remember the difference between break and continue statements.
  • Use conditional loops to control the flow of your iterations.
  • Nested loops can be useful for complex data structures.
  • Visualize the execution of your loops to better understand how they work.

...And now, for the suspenseful question: What happens when you combine conditional loops with switch statements? Stay tuned for the next section, Breaking and Continuing Loops, to find out!

Breaking and Continuing Loops

Control the Flow of Your Loops

Loops are like the workhorses of programming, but sometimes you need to take control and adjust their course. That's where breaking and continuing loops come in.

Break: The Quick Exit

Imagine you're searching for a specific item in an array. As soon as you find it, you don't want to keep looping through the rest of the array. That's when you use the break statement.

Code Example:

const numbers = [1, 2, 3, 4, 5, 10];

for (let i = 0; i < numbers.length; i++) { if (numbers[i] === 10) { break; } }

console.log("Found the element at index: " + i);

Continue: The Selective Skipping

Sometimes you don't want to stop the loop, but you do want to skip certain iterations. That's where the continue statement comes in handy.

Code Example:

const evenNumbers = [];

for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { evenNumbers.push(i); continue; } }

console.log("Even numbers: " + evenNumbers);

Visualizing the Flow

To understand how breaking and continuing loops affect the flow of your program, think of a loop as a series of steps:

  • Check the condition
  • Execute the body
  • Next iteration

When you use break, you skip all remaining steps and exit the loop. When you use continue, you skip the remaining steps within the current iteration and move on to the next one.

Tips for Using Breaking and Continuing Loops

  • Use break when you want to leave the loop immediately after a specific condition is met.
  • Use continue when you want to skip certain iterations based on a condition.
  • Keep in mind that breaking and continuing can alter the expected behavior of your loop.
  • Visualize the steps to understand how these statements affect the flow.

...And now, a suspenseful question: What happens when we combine loops with conditional statements? Stay tuned for the next section, Nested Loops and Complex Flow, to find out how you can add even more control to your code!

Nested Loops and Complex Flow

In programming, loops are used to perform repeated tasks a specified number of times or until a certain condition is met. When you need to create more complex control flow, you can use nested loops.

Nested Loops

Nested loops are loops within loops. This allows you to iterate through multiple sets of data simultaneously. For example, you could use nested loops to iterate through a table of numbers, performing a different calculation for each row and column.

// Iterate through a 2D array
for (let i = 0; i < table.length; i++) {
  for (let j = 0; j < table[i].length; j++) {
    // Perform calculation for each cell
    const result = table[i][j] * 2;

Complex Control Flow

Combining loops with conditional statements, such as if...else statements and switch statements, allows you to create even more complex control flow. For instance, you could use a loop to iterate through an array and conditionally perform different actions for each element.

// Iterate through an array and conditionally print each element
for (let i = 0; i < array.length; i++) {
  if (array[i] > 0) {
    console.log("Positive number: " + array[i]);
  } else if (array[i] < 0) {
    console.log("Negative number: " + array[i]);
  } else {

Tips for Using Nested Loops and Complex Flow

  • Keep your loops organized and readable.
  • Use descriptive variable names to make your code easier to understand.
  • Test your code thoroughly to ensure that it works as expected.
  • Refactor your code to improve readability and maintainability.

Example Scenario

Imagine you have a list of shopping lists, and each shopping list contains a list of items. You want to iterate through the shopping lists and print out each item that appears on more than one list.

You can use nested loops to accomplish this:

// Iterate through each shopping list
for (let i = 0; i < shoppingLists.length; i++) {
  // Iterate through each item in the current shopping list
  for (let j = 0; j < shoppingLists[i].length; j++) {
    // Check if the current item appears in any other shopping list
    let count = 0;
    for (let k = 0; k < shoppingLists.length; k++) {
      if (shoppingLists[k].includes(shoppingLists[i][j])) {

// If the current item appears in more than one shopping list, print it
if (count &gt; 1) {

} }


Nested loops and complex flow can add power and flexibility to your JavaScript programs. By mastering these concepts, you'll be able to create more sophisticated and efficient code.

Suspenseful Question

What happens when you use a loop within a loop within a loop? Stay tuned for the next section, Refactoring Control Structures for Readability, to find out!

Refactoring Control Structures for Readability

Control structures are a crucial aspect of JavaScript, allowing you to guide the flow of your code's execution. However, writing readable and maintainable control structures can be challenging. In this section, we'll explore ways to refactor your control structures to improve their readability and clarity.

Practical Tips for Refactoring

1. Extract Nested If Statements:

If your code contains multiple nested if statements, consider extracting them into separate functions or methods. This breaks down the complexity and makes it easier to read and maintain.

2. Use Case Statements Instead of if-else Chains:

Case statements provide a concise alternative to long if-else chains. Each case corresponds to a specific condition, making the code much more readable.

3. Use Conditionals with Early Exit:

If you have a series of conditions that must be met in a specific order, consider using conditionals with early exit. This approach ensures that subsequent conditions are only checked if the previous ones are satisfied.

4. Reduce Complexity with Loops:

In some cases, loops can be used to simplify complex control structures. For example, instead of using multiple nested if statements to iterate through an array, you can use a for loop to achieve the same result.

5. Visualize the Control Flow:

Create a visual representation of your control flow using flowcharts, diagrams, or tables. This helps identify potential issues and makes it easier to optimize the structure.

Example: Refactoring a Nested If Statement

Consider the following nested if statement:

if (condition1) {
  if (condition2) {
    // Code to execute if both conditions are true
  } else {
    // Code to execute if only condition1 is true
} else if (condition3) {
  // Code to execute if condition3 is true
} else {
  // Code to execute if no conditions are met

This can be refactored into separate functions as follows:

function checkCondition1() {
  return condition1;

function checkCondition2() { return condition2; }

function checkCondition3() { return condition3; }

if (checkCondition1()) { if (checkCondition2()) { // Code to execute if both conditions are true } else { // Code to execute if only condition1 is true } } else if (checkCondition3()) { // Code to execute if condition3 is true } else { // Code to execute if no conditions are met }


Refactoring control structures is an essential skill for writing readable and maintainable JavaScript code. By following the tips discussed in this section, you can improve the clarity and organization of your control flow, making it easier to understand and maintain.

Error Handling with try...catch...finally


Error handling is crucial for ensuring the robustness and stability of your JavaScript applications. The try...catch...finally block provides a structured way to handle unexpected errors and respond appropriately.

How it Works:

The try block contains the code that is potentially error-prone. If an error occurs within the try block, control is transferred to the catch block. The catch block is responsible for handling the error and providing a custom response. Finally, the finally block is always executed, regardless of whether an error occurred or not.

Practical Uses:

  • Fault Tolerance: Enable your applications to gracefully handle and recover from errors, ensuring uninterrupted operation.
  • Debug and Diagnostics: Error handling allows you to capture error messages and provide descriptive information to diagnose issues efficiently.
  • User Experience: Custom error messages can improve user experience by providing clear and actionable guidance during unexpected scenarios.


Consider a function that performs a file read operation:

try {
  const data = readFile("data.txt");
} catch (error) {
  console.error("Error reading file:", error.message);
} finally {
  console.log("File handling complete.");

In this example:

  • If the file read operation is successful, the data is stored in the data variable.
  • If an error occurs while reading the file, the catch block captures the error and logs the error message.
  • The finally block is executed regardless of the outcome, providing a standardized cleanup or logging of the file operation.


  • Use descriptive error messages to provide context and guidance.
  • Consider providing multiple catch blocks to handle specific types of errors differently.
  • Use the finally block for cleanup operations, such as closing resources or logging important information.
  • Don't forget the finally block, as it's crucial for ensuring proper resource cleanup and providing a consistent exit point.

Suspenseful Question:

What happens when multiple errors occur within nested try...catch blocks? Stay tuned for our next section to explore the intricacies of error handling in such scenarios!

Async Control Flow with Promises and async/await

Promises: A Glimpse into the Future

In the world of asynchronous programming, where code executions occur independently, promises are a powerful tool for managing the flow of events. Imagine a scenario where you send a request to a server and you don't want to wait for the response before continuing your program. Promises allow you to define a function that will be executed once the server responds, guaranteeing that your code will run even if the server takes time to respond.

async/await: A Simplified Approach

Async/await is a game-changer in asynchronous programming, making it look and feel like synchronous code. With async/await, you can write asynchronous code in a sequential manner, as if waiting for the results to be available. The syntax is straightforward: simply use the async keyword before a function and the await keyword before a promise.

Example: Fetching User Data

Consider this code snippet:

async function getUserData() {
  let response = await fetch('');
  let data = await response.json();

Using async/await, we can fetch user data from a server and wait for the response without blocking the main program flow. This makes the code more readable and maintainable.

Practical Tips for Async Control Flow

  • Understand the Concept of Promises and async/await: Grasp their core principles to harness their power effectively.
  • Use Promises for Asynchronous Operations: Identify the parts of your code that need to run independently and utilize promises to manage their execution.
  • Leverage async/await for Easy Async Code: When possible, switch to async/await for a simpler and more sequential approach.
  • Avoid Nested Promises and Callbacks: Keep your code structure tidy by using async/await to resolve nested promises and eliminate excessive callbacks.

Concluding Thoughts

Async control flow with promises and async/await empowers you to write asynchronous code efficiently and maintainably. These techniques offer a structured and intuitive way to handle asynchronous operations, enabling your code to respond dynamically to events and run smoothly, even when dealing with delayed or unexpected server responses.

Using Control Structures to Solve Common Coding Challenges


Control structures are the backbone of any programming language. They allow you to execute code conditionally or repeatedly, depending on the input data. In this subheading, we'll explore practical ways to use control structures to solve common coding challenges.

Using if...else Statements for Decision Making:

Imagine a game where you have two options: attack or defend. Using an if...else statement, you can determine which option to take based on the player's or enemy's actions. For example:

if (playerHP <= 0) {
  console.log("Game over!");
} else {
  console.log("Player still standing!");

Using switch Statements for Multi-Branch Decisions:

In a restaurant ordering system, you need to handle multiple food item requests. A switch statement allows you to easily specify the action for each food item:

switch (foodItem) {
  case "pizza":
    console.log("Preparing pizza!");
  case "burger":
    console.log("Grilling burger!");
    console.log("Sorry, we don't have that.");

Using for Loops for Iteration:

To count the number of items in a shopping cart, you can use a for loop:

let numItems = 0;
for (let i = 0; i < cart.length; i++) {
console.log("Total items:", numItems);

Using While and do...while Loops for Conditional Iteration:

To keep attacking an enemy until its health reaches 0, you can use a while loop:

while (enemyHP > 0) {
  console.log("Attacking enemy!");
  enemyHP -= 10;


  • Use if...else statements for conditional execution.
  • Use switch statements for multi-branch decisions.
  • Use for loops for iteration.
  • Use while and do...while loops for conditional iteration.

Quiz: Practice with Control Structures in JavaScript

Now that you know the basics, test your skills with a coding quiz! Solve real-world problems using control structures. Prepare to be challenged...

Quiz: Practice with Control Structures in JavaScript

Consider Yourself a Control Structure Master?

Put your JavaScript skills to the test with this coding quiz. Challenge yourself to solve real-world problems using control structures like if...else, switch, for, while, and do...while.

Let's Play!

  1. A superhero has a power level that ranges from 1 to 10. Write a function that displays their power level using an if...else statement.

  2. Create a switch statement to simulate a restaurant ordering system. When a customer orders a dish (e.g. "pizza" or "pasta"), print the corresponding preparation message.

  3. Use a for loop to iterate through an array of strings and print each element.

  4. Write a while loop to calculate the factorial of a given number.

  5. Construct a do...while loop to continue asking a user for input until they enter "yes".

Don't be afraid to experiment and apply your control structure knowledge!


  • if...else: Conditional execution
  • switch: Multi-branch decisions
  • for: Iteration
  • while: Conditional iteration
  • do...while: Conditional iteration

Good luck with the quiz and may your control structures flow flawlessly!

Share Button