JS Conditional Statements

JavaScript Conditional Statement

Conditional statements in JavaScript allow you to make decisions in your code. They help your program choose between different actions based on whether a certain condition is true or false.
In simple terms, it’s like asking a question. If the answer is “yes,” the program does one thing. If the answer is “no,” it does something else.
For example:
• If it is raining, then take an umbrella.
• If it is not raining, then go outside without an umbrella.
There are various types of Conditional statements in JavaScript.
• if statement
• if-else statement
• if-else-if ladder statement

1. If Statement

An if statement is used to check a condition. If the condition is true, the code inside the if block will run. If the condition is false, the code will be skipped.

Basic Structure

if (condition) {
    // code for execution
}

Scenario 1: Write a JavaScript program that checks if a person is eligible to create a bank account based on their age. If the age is greater than or equal to 18, it should print “You can create your BankAccount” in the console.

let age = 20;
if (age >= 18) {
    console.log(“You can create your BankAccount”);
}

Output:
Code Explanation

let age = 20;

This line creates a variable named age and assigns it the value 20. In this case, it means the person is 20 years old.

if (age >= 18)

This is an if statement, which checks a condition. The condition being checked here is whether the value of age is greater than or equal to 18.
If the condition is true (meaning the person is 18 or older), the code inside the curly brackets { } will be executed.
If the condition is false (meaning the person is younger than 18), nothing will happen, and the code inside the { } will be skipped.

{ console.log(“You can create your BankAccount”); }

This line is inside the if block. If the condition is true (i.e., the person’s age is 18 or older), this code will run.
It uses console.log() to print the message “You can create your BankAccount” to the console (the output area where developers can see messages).

2. If-Else Statement

An if-else statement provides an alternative action when the condition is false. It consists of an if block and an else block.

Basic Structure

if (condition) {
     // code for execution
} else {
    // code execution if a condition is false
}

Scenario 2: Write a JavaScript program that checks if a person is eligible to create a bank account based on their age. The program should print a different message if the person is under 18. If the condition is true, print “You can create your BankAccount” to the console.

If the condition is false, print “You can’t create your BankAccount, your age is less than 18” to the console.

let age = 11;
if (age >= 18) {
    console.log(“You can create your BankAccount”);
} else {
    console.log(“You can’t create your BankAccount, your age is less than 18”);
}

Output:
Code Explanation

let age = 11;

This line creates a variable named age and assigns it the value 11. In this case, it means the person is 11 years old.

if (age >= 18){

This is an if statement that checks whether the age is greater than or equal to 18.
If this condition is true (i.e., the person is 18 or older), the code inside the first { } will be executed.

{ console.log(“You can create your BankAccount”); }

If the person is 18 or older, this line will run and print the message “You can create your BankAccount” to the console.

else{

If the if condition is false (i.e., the person is younger than 18), the code inside the else block will be executed instead.

{ console.log(“You can’t create your BankAccount, your age is less than 18”); }

Since the person is younger than 18 (in this case, 11), this message will be printed to the console: “You can’t create your BankAccount, your age is less than 18”.

3. If-Else-If Ladder Statement

An if-else-if ladder statement allows you to check multiple conditions in sequence. It helps to make more complex decisions by testing several possibilities.

Basic Structure

if (condition) {
    // code for execution
} else if (condition) {
    // code for execution if first condition is false
} else {
    // default code execution if all conditions are false
}

Scenario 3: Write a JavaScript program that checks a person’s age and categorizes them as a Senior Citizen, eligible to create a bank account, or too young to create a bank account by using the if-else ladder Statement
If the age is 60 or older, print “You are a Senior Citizen.”
If the age is 18 or older but less than 60, print “You can create your BankAccount.”
If the age is less than 18, print “You can’t create your BankAccount. Your age is less than 18.”

let age = 30;
if (age >= 60) {
    console.log(“You are a Senior Citizen.”);
} else if (age >= 18) {
    console.log(“You can create your BankAccount.”);
} else {
    console.log(“You can’t create your BankAccount. Your age is less than 18.”);
}

Output:
Code Explanation

let age = 30;

This line creates a variable named age and assigns it the value 30. In this case, it means the person is 30 years old.

if (age >= 60)

The first condition checks if the person’s age is 60 or older.
If this condition is true, the code inside this block will run and print “You are a Senior Citizen.” to the console.

else if (age >= 18)

If the first condition (age >= 60) is false, the program moves to this else if condition.
It checks if the person’s age is greater than or equal to 18 but less than 60.
If this condition is true, the code inside this block will run and print “You can create your BankAccount.”.

Course Video

YouTube Reference :

Practice Scenarios

Scenario 1: Driving Eligibility (If Statement)

Objective: Use an if statement to determine if a person is eligible to drive based on their age.
Expected Output: A message indicating whether the person is eligible to drive.
age = 19, condition: age >= 18

Output

Scenario 2: Pass or Fail (If-Else Statement)

Objective: Use an if-else statement to determine if a student passes or fails based on their score.
Expected Output: A message indicating whether the student has passed or failed.
score = 45, condition: score >= 50

Output

Scenario 3: Grade Calculation (If-Else-If Ladder Statement)

Objective: Use an if-else-if ladder statement to assign grades to a student based on their exam score.
Expected Output: A message showing the grade based on the score.
score = 82, Conditions: (score >= 90 Grade:A), (score >= 80 Grade:B), (score >= 70 Grade:C), (score >= 60 Grade:D), (else Grade:F).

Output

Scenario 4: Voting Eligibility (If Statement)

Objective: Use an if statement to check if someone is eligible to vote.
Expected Output: A message indicating whether the person is eligible to vote or not.
age = 18, Condition: age >= 18.

Output

Scenario 5: Temperature Check (If-Else Statement)

Objective: Use an if-else statement to determine if the temperature is hot or cold based on a temperature value.
Expected Output: A message indicating if the temperature is hot or cold.
temperature = 30, Condition: temperature >= 25.

Output

Scenario 6: Scholarship Eligibility (If-Else-If Ladder Statement)

Objective: Use an if-else-if ladder statement to determine if a student qualifies for a scholarship based on their grades.
Expected Output: A message indicating the scholarship type the student is eligible for.

Code Example:

let grade = 88;
if (grade >= 90) {
    console.log(“You are eligible for a full scholarship.”);
} else if (grade >= 80) {
    console.log(“You are eligible for a partial scholarship.”);
} else if (grade >= 70) {
    console.log(“You are eligible for a merit-based scholarship.”);
} else {
    console.log(“You are not eligible for a scholarship.”);
}

Scenario 7: Checking for Even or Odd Numbers (If-Else Statement)

Objective: Use an if-else statement to determine if a number is even or odd.
Expected Output: A message indicating if the number is even or odd.
number = 23, Condition: number % 2 === 0.

Output
Chatbot