Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 3 - Control Flow

3.1 Conditional Statements

Conditional statements allow your program to make decisions, done by combining if, else if, and else statements. Where:

  • The if statement executes a code block when the condition is true.
  • The else if statement is an additional condition that executes a code block when the if statement is false, but the additional statement is true.
  • The else statement works like a default action, executing a code block when both the if and else if statements are false.

Take a look at the following example:

let x = 10;

if (x > 10) {
    console.log("x is greater than 10");
} else if (x < 10) {
    console.log("x is less than 10");
} else {
    console.log("x is equal to 10");
}

Here, we compare the value of x with 10. The code first checks if x > 10, then checks if x < 10. If both of the conditions are false, it executes the default action using the else statement.

Now try to change the value of x.