Day 7 of #100daysofjs

Conditional expressions in JavaScript

Sometimes we might have to execute a block of code based off some condition.

For example, a prompt might ask for the age of a user and if it's greater than 18, display a special message.

In JavaScript, we have three forms of if .... else statement

  1. if statement.

  2. else if statement.

  3. else statement.

if statement

In JavaScript, the if statement is a conditional statement that allows you to execute a block of code if a specified condition is true. It has the following syntax:

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

Here's how it works:

  1. The condition is an expression that evaluates to either true or false.

  2. If the condition is true, the code block within the curly braces {} following the if statement is executed.

  3. If the condition is false, the code block is skipped, and the program continues with the next statement after the if block.

Here's an example that demonstrates the usage of the if statement:

var age = 20;

if (age >= 18) {
  console.log("You are eligible to vote.");
}

In this example, if the value of age is greater than or equal to 18, the message "You are eligible to vote." will be printed to the console.

The if statement can be used alone or in combination with other conditional statements like else or else if to handle multiple conditions.

var num = 10;

if (num > 0) {
  console.log("The number is positive.");
} else if (num < 0) {
  console.log("The number is negative.");
} else {
  console.log("The number is zero.");
}

In this example, if the value of num is greater than 0, the message "The number is positive." will be printed. If it's less than 0, the message "The number is negative." will be printed. If none of the conditions is true (i.e., the number is zero), the message "The number is zero." will be printed.

In JavaScript, the else if statement is used in conjunction with the if statement to test additional conditions after the initial if condition evaluates to false. It allows you to create a chain of conditional statements to handle multiple cases. The syntax is as follows:if else if else statement.

else if statement

if (condition1) {
  // code to be executed if condition1 is true
} else if (condition2) {
  // code to be executed if condition2 is true
} else {
  // code to be executed if all conditions are false
}

Here's how it works:

  1. The condition1, condition2, and so on, are expressions that evaluate to either true or false.

  2. If the condition1 in the initial if statement is false, the program moves to the next else if statement and checks condition2.

  3. If condition2 is true, the code block within the corresponding else if block is executed, and the program skips the remaining else if and else blocks.

  4. This sequence continues for each subsequent else if statement.

  5. If none of the conditions in the if and else if statements are true, the code block within the else block is executed.

Here's an example that demonstrates the usage of the else if statement:

var num = 10;

if (num > 0) {
  console.log("The number is positive.");
} else if (num < 0) {
  console.log("The number is negative.");
} else {
  console.log("The number is zero.");
}

In this example, if the value of num is greater than 0, the message "The number is positive." will be printed. If it's less than 0, the message "The number is negative." will be printed. If none of the conditions is true (i.e., the number is zero), the message "The number is zero." will be printed.

You can have multiple else if statements to handle additional conditions as needed, creating a chain of conditions to cover various cases.

else statement.

In JavaScript, the else statement is used in conjunction with the if statement to specify a block of code that should be executed when the condition of the if statement evaluates to false. It provides an alternative code path when the condition is not met. The syntax is as follows:

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

Here's how it works:

  1. The condition is an expression that evaluates to either true or false.

  2. If the condition in the if statement is true, the code block within the corresponding curly braces {} is executed.

  3. If the condition is false, the code block within the else block is executed instead.

Here's an example that demonstrates the usage of the else statement:

var age = 15;

if (age >= 18) {
  console.log("You are eligible to vote.");
} else {
  console.log("You are not eligible to vote.");
}

In this example, if the value of age is greater than or equal to 18, the message "You are eligible to vote." will be printed. If the condition is false (i.e., the age is less than 18), the message "You are not eligible to vote." will be printed.

The else statement is optional, and you can use it after the if statement to provide an alternative code path when the condition is not true. It helps you handle both cases of the condition (true and false) and provides a fallback option when the condition is not met.

Did you find this article valuable?

Support Arpan Mukherjee by becoming a sponsor. Any amount is appreciated!