JS Conditional

JavaScript Conditional Statements

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:

There are various types of conditional statements in JavaScript:


1. JavaScript 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.

if Statement Syntax

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.

if Statement Example

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

if Statement Explanation


2. JavaScript 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.

if-else Statement Syntax

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-else Statement Example

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

if-else Statement Explanation


3. JavaScript 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.

if-else-if Ladder Syntax

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

Scenario 3: Write a JavaScript program that categorizes a person as a Senior Citizen, eligible to create a bank account, or too young to create a bank account using the if-else ladder statement.

if-else-if Ladder Example

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

if-else-if Ladder Explanation


4. JavaScript Conditional Operator (? :)

The conditional operator (often called the ternary operator) first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.

Conditional Operator Syntax

condition ? valueX : valueY

If the condition is true, then the result is valueX. Otherwise, the result is valueY.

Conditional Operator Example

let a = 10;
let b = 20;

console.log("((a > b) ? 100 : 200) => " + ((a > b) ? 100 : 200)); console.log("((a < b) ? 100 : 200) => " + ((a < b) ? 100 : 200));


5. The switch Statement

Use the switch statement to specify many alternative blocks of code to be executed. It provides a more elegant way to handle multiple if-else-if conditions by evaluating an expression and executing code blocks based on a matching case.

switch Statement Syntax

switch(expression) {
  case value1:
    // code block to be executed if expression matches value1
    break;
  case value2:
    // code block to be executed if expression matches value2
    break;
  default:
    // code block to be executed if expression doesn't match any case
}

How It Works

switch Statement Example

let dayNumber = 3;
let dayName;

switch (dayNumber) { case 0: dayName = "Sunday"; break; 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; default: dayName = "Invalid day"; } console.log("The day is " + dayName);


Exercise

?

Which statement is used to check multiple conditions in sequence in JavaScript?