Loading...
In this course, you'll learn about JavaScript control structures. We'll cover conditional statements, loops, and switch statements - essential tools for controlling the flow of your code and making decisions based on different conditions.
Learn about if, else if, and else
Learn MoreUnderstand for, while, and do-while loops
Learn MoreMaster the switch-case structure
Learn MoreControl structures are the backbone of programming. They allow you to make decisions in your code, repeat actions, and choose different code paths based on conditions. Mastering these concepts will give you the power to create more complex and interactive JavaScript applications.
The if-else statement allows you to execute different blocks of code based on a condition.
let age = 18;
if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote yet.");
}
You are eligible to vote.
In this example, since the age is 18, which is greater than or equal to 18, the first condition is true, so the first console.log statement is executed.
Best Use Case
Use if-else statements when you need to execute different code blocks based on a single condition. It's particularly useful for binary decisions or simple branching logic.
The else-if statement allows you to check multiple conditions in sequence.
let score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
Grade: B
Here, the score is 85. It's not greater than or equal to 90, but it is greater than or equal to 80, so the second condition is true and 'Grade: B' is logged.
Best Use Case
Use else-if statements when you need to check multiple conditions in a specific order. It's ideal for scenarios where you have multiple possible outcomes based on different conditions, such as grading systems or categorization tasks.
The for loop repeats a block of code a specified number of times.
for (let i = 0; i < 5; i++) {
console.log("Iteration: " + i);
}
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
This for loop runs 5 times, with i starting at 0 and incrementing by 1 each time until it reaches 4.
Best Use Case
Use for loops when you know the exact number of iterations needed. They're ideal for iterating over arrays or performing a task a specific number of times.
The while loop repeats a block of code as long as a specified condition is true.
let count = 0;
while (count < 5) {
console.log("Count: " + count);
count++;
}
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
This while loop continues to execute as long as count is less than 5. It increments count each time, so it will run 5 times.
Best Use Case
Use while loops when you don't know in advance how many times the loop should run. They're great for situations where you need to continue a process until a certain condition is met.
The do-while loop is similar to the while loop, but it always executes the code block at least once before checking the condition.
let num = 0;
do {
console.log("Number: " + num);
num++;
} while (num < 5);
Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
This do-while loop works similarly to the while loop, but it would execute at least once even if the initial condition was false.
Best Use Case
Use do-while loops when you want to ensure that the code block is executed at least once, regardless of the condition. This is useful for scenarios where you need to perform an action before checking if it should be repeated.
The switch statement is used to perform different actions based on different conditions.
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
default:
dayName = "Weekend";
}
console.log("Today is " + dayName);
Today is Wednesday
In this switch statement, the value of day is 3, so it matches the case 3, and dayName is set to 'Wednesday'.
Best Use Case
Use switch statements when you have multiple conditions to check against a single variable. It's especially useful for menu systems, state machines, or when mapping numeric values to string representations (like in this day of the week example).
We use cookies to ensure you get the best experience on our website. By continuing, you agree to our use of cookies.