JavaScript if...else Tutorial

What are Conditional Statements?

  1. Conditional statements are used in programming to make decisions.
  2. They execute different blocks of code based on whether a specified condition is true or false.
  3. JavaScript provides several types of conditional statements, including if, if-else, if-else if-else, and switch.

if statement

  1. The "if" statement runs a block of code only if a specified condition is true.

Let's see some examples

let age = 19;
if(age === 19){
    console.log("Hey, You can Vote Now");
}

//Output: Hey, You can Vote Now

The above code runs successfully because the variable "age" has a value of 19. We check if the "age" value is equal to 19. If this condition is true, we execute the code within the if block. The code within the block can be as simple as logging a message to the console or more complex tasks like user login to the system and so on.

let evenNum;
let num = 10;
if(num%2 === 0){
    evenNum = num;
    console.log("If this true it will executes")
}
console.log(evenNum)

// Output
// If this true it will executes
// 10

if...else tutorial

  1. The "if" statement runs a block of code when a condition is true. If the condition is false, it doesn't do anything.
  2. But if you want to execute a statement when the condition is false, you can use an "if...else" statement.
const num = 9;

if (num % 2 === 0) {
    console.log("The number is even.");
} else {
    console.log("The number is odd.");
}
    

This code will print the following output to the console The number is odd.

else if

  1. You can also use the else if statement to check multiple conditions and execute different code blocks depending on the outcome of each condition.

Syntax:

if (condition1) {
    // Code to be executed if condition1 is true
} else if (condition2) {
    // Code to be executed if condition1 is false and condition2 is true
} else {
    // Code to be executed if condition1 and condition2 are both false
}

Example:

const age = 18;

if (age >= 18) {
    console.log("You are an adult.");
} else if (age >= 13) {
    console.log("You are a teenager.");
} else {
    console.log("You are a child.");
}
    

You can also add more else if statements as you need them in your application.

This code will print the following output to the console You are an adult.

Nested if statements

  1. Nested if statements are used to create more complex conditional logic in JavaScript.
  2. They allow you to check multiple conditions and execute different code blocks depending on the outcome of each condition.

Syntax:

if (condition1) {
// Code to be executed if condition1 is true
if (condition2) {
      // Code to be executed if condition1 is true and condition2 is true
} else {
      // Code to be executed if condition1 is true and condition2 is false
}

} else {
    // Code to be executed if condition1 is false
}
  

Example:

const isLoggedIn = true;
const isAdmin = false;

if (isLoggedIn) {
    if (isAdmin) {
    console.log("You are logged in as an administrator.");
    } else {
    console.log("You are logged in as a standard user.");
    }
} else {
    console.log("You are not logged in.");
}

The output is You are logged in as a standard user.

Switch Statement

  1. The JavaScript switch statement is a control flow statement that allows you to execute different code blocks based on the value of an expression.
  2. It is an easiest to write multiple if/else statements, especially when there are many possible values for the expression.

Syntax:

switch (expression) {
case value1:
    // Code to be executed if the expression evaluates to value1
    break;
case value2:
    // Code to be executed if the expression evaluates to value2
    break;
...
default:
    // Code to be executed if the expression does not evaluate to any of the other cases
}
  

The expression can be any expression that evaluates to a value. The value1, value2, and so on can be any value that the expression can evaluate to.

The break statement is used to prevent the code in the switch statement from executing the following cases. If you omit the break statement, the code in the switch statement will continue to execute the following cases until it reaches a break statement or the end of the switch statement.

The default case is optional. It is used to execute code if the expression does not evaluate to any of the other cases.

Example:

const dayOfWeek = 3;

switch (dayOfWeek) {
    case 0:
        console.log("Today is Sunday.");
        break;
    case 1:
        console.log("Today is Monday.");
        break;
    case 2:
        console.log("Today is Tuesday.");
        break;
    case 3:
        console.log("Today is Wednesday.");
        break;
    case 4:
        console.log("Today is Thursday.");
        break;
    case 5:
        console.log("Today is Friday.");
        break;
    case 6:
        console.log("Today is Saturday.");
        break;
    default:
        console.log("Invalid day of the week.");
}
    

The output of the following code will be Today is Wednesday.

const fruit = "banana";

switch (fruit) {
    case "apple":
        console.log("You chose an apple.");
        break;
    case "banana":
        console.log("You chose a banana.");
        break;
    case "orange":
        console.log("You chose an orange.");
        break;
    default:
        console.log("You chose an unknown fruit.");
}
    

The output of the following code will be You chose a banana.