Instagram
youtube
Facebook
Twitter

Conditional Statements

Conditional Statements

Conditional statements in JavaScript allow you to perform different actions based on different conditions. These statements enable your code to make decisions and execute certain blocks of code only when specific conditions are met.


If Statement

The if statement is the most basic form of conditional statement in JavaScript. It allows you to execute a block of code only if a specified condition is true.

let age = 18;

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

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


Else Statement

The else statement is used to specify a block of code that will be executed if the condition in the if statement is false.

let age = 16;

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

Here, since the value of age is 16, the message "You are not eligible to vote." will be printed.


Else If Statement

The else if statement is used to specify a new condition to test if the first condition is false. You can use multiple else if statements to check several conditions in sequence.

let score = 75;

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");
}

In this example, since the score is 75, the message "Grade: C" will be printed.


Switch Statement

The switch statement is another way to perform conditional operations, especially when you need to compare a single expression against multiple possible values. It is often used as an alternative to a series of else if statements.

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;
  case 6:
    dayName = "Saturday";
    break;
  case 7:
    dayName = "Sunday";
    break;
  default:
    dayName = "Invalid day";
}

console.log(dayName); // Outputs: Wednesday

In this example, the switch statement compares the value of day and assigns the corresponding day name to the dayName variable. The break keyword prevents the code from continuing to execute subsequent cases after a match is found.


Conclusion

Conditional statements are crucial for controlling the flow of your JavaScript programs. By mastering if, else, else if, and switch statements, you can create dynamic and responsive code that reacts to different situations effectively.


FAQs

Question: Write a JavaScript program to check if a given number is positive.

let number = 10;

if (number > 0) {
    console.log("The number is positive.");
}

 

Question: Write a JavaScript program to check if a number is even or odd.

let number = 5;

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

 

Question: Write a JavaScript program to check if a number is positive, negative, or zero

let number = 0;

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

 

Question: Write a JavaScript program that takes a character representing a grade (A, B, C, D, F) and prints a message indicating the performance level (Excellent, Good, Average, Poor, Fail).

let grade = 'B';

switch (grade) {
    case 'A':
        console.log("Excellent");
        break;
    case 'B':
        console.log("Good");
        break;
    case 'C':
        console.log("Average");
        break;
    case 'D':
        console.log("Poor");
        break;
    case 'F':
        console.log("Fail");
        break;
    default:
        console.log("Invalid grade.");
}