Learn JavaScript Day 2: Understanding Conditional Statements and Loops in JavaScript

Date:

Share post:

Learn JavaScript Day 2: Understanding Conditional Statements and Loops in JavaScript

Congratulations on completing Day 1 of your journey to becoming a skilled JavaScript developer! Today, we’ll be diving into one of the most important concepts in programming: conditional statements and loops.

Conditional Statements

Conditional statements are an essential tool for making decisions in your code. With these statements, you can execute certain code blocks based on specific conditions. For example, you can use conditional statements to check if a number is positive or negative, and then execute code accordingly.

In JavaScript, there are three types of conditional statements: if, else, and switch. Let’s take a closer look at each one.

if Statements

if statements are the simplest and most commonly used type of conditional statement. They allow you to execute code only if a certain condition is met. The syntax for an if statement is as follows:

if (condition) {
  // code to be executed if the condition is true
}

For example, consider the following code:

let num = 5;

if (num > 0) {
  console.log(`${num} is a positive number`);
}

In this example, the code inside the if statement will be executed only if the condition num > 0 is true. In this case, num is indeed greater than 0, so the output will be 5 is a positive number.

else Statements

else statements are used to provide an alternative code block to be executed if the condition specified in the if statement is not met. The syntax for an if-else statement is as follows:

if (condition) {
  // code to be executed if the condition is true
} else {
  // code to be executed if the condition is false
}

For example, consider the following code:

let num = -5;

if (num > 0) {
  console.log(`${num} is a positive number`);
} else {
  console.log(`${num} is a negative number`);
}

In this example, the code inside the if statement will not be executed because the condition num > 0 is false. Instead, the code inside the else statement will be executed, and the output will be -5 is a negative number.

switch Statements

switch statements are another type of conditional statement used to execute code based on the value of an expression. The syntax for a switch statement is as follows:

switch (expression) {
  case value1:
    // code to be executed if the expression is equal to value1
    break;
  case value2:
    // code to be executed if the expression is equal to value2
    break;
  ...
  default:
    // code to be executed if the expression does not match any of the values
}

For example, consider the following code:

let day = "Monday";

switch (day) {
    case "Monday":
        console.log("Today is Monday");
        break;
    case "Tuesday":
        console.log("Today is Tuesday");
        break;
    case "Wednesday":
        console.log("Today is Wednesday");
        break;
    default:
        console.log("Today is another day");
}

In this example, the switch statement evaluates the value of the variable day. If the value of day is “Monday”, the code inside the first case will be executed and the output will be Today is Monday. If the value of day does not match any of the specified values, the code inside the default case will be executed and the output will be Today is another day.

Loops

Now that you have a good understanding of conditional statements in JavaScript, let’s move on to loops. Loops are used to repeatedly execute a block of code until a specified condition is met.

There are three types of loops in JavaScript: for loops, while loops, and do-while loops.

For Loop

For loops are used to execute a block of code a specified number of times. The syntax for a for loop is as follows:

for (initialization; condition; increment/decrement)
{
// code to be executed 
}

For example, consider the following code:

for (let i = 0; i < 5; i++) {
    console.log(${i} is the current iteration);
}

In this example, the code inside the for loop will be executed five times. On each iteration, the value of i will be incremented by 1 until it reaches 5. The output will be:

0 is the current iteration 1 is the current iteration 2 is the current iteration 3 is the current iteration 4 is the current iteration

While Loops

While loops are used to execute a block of code as long as a specified condition is true. The syntax for a while loop is as follows:

while (condition)
{ // code to be executed 
}

For example, consider the following code:

let i = 0;
while (i < 5) {
    console.log(${i
} 
is the current iteration);
    i++;
}

In this example, the code inside the while loop will be executed as long as the condition i < 5 is true. On each iteration, the value of i will be incremented by 1 until it reaches 5. The output will be the same as the previous example.

Do-While Loops

Do-while loops are similar to while loops, but the difference is that the code inside the do-while loop is executed at least once, regardless of the condition. The syntax for a do-while loop is as follows:

do {
// code to be executed 
} 
while (condition);

For example, consider the following code:

let i = 0;

do {
    console.log(${
i
}
 is the current iteration);
    i++;
} 
while (i < 5);

In this example, the code inside the do-while loop will be executed once, and then the condition i < 5 will be evaluated. If the condition is true, the code inside the loop will be executed again. The output will be the same as the previous two examples.

Conclusion

In conclusion, understanding conditional statements and loops is crucial for building robust and efficient JavaScript applications. By mastering these concepts, you’ll be well on your way to becoming a skilled JavaScript developer. Don’t forget to practice what you’ve learned today and continue your journey to JavaScript mastery!

Subscribe to our newsletter

Stay ahead of the game! Subscribe to our newsletter for exclusive tips and insights on Data Structures & Algorithms and interview preparation.

Leave a Reply

Related articles

10 Effective Growth Hacking Techniques to Boost Your Online Influence**

The Influence of Online Power: 10 Techniques for Boosting Growth In today's digital world, having a strong online presence...

Boost Your Productivity with Checklists: An Essential Tool for Every Blogger and Marketer

The Power of Using Checklists: Enhancing Your Efficiency as a Blogger or Marketer In the fast-paced world we live...

Convert Webpages to PDFs: A Simple Guide**

Finding an easy and reliable way to convert webpages to PDF files can be a daunting task. Many...

Mastering Freelance Success: Key Tips for Building a Thriving Career

Keys to Creating a Successful Freelance Business: Essential Strategies and Techniques Introduction: Flourishing in the Freelance Economy As the gig...