If else conditions in Javascript

If Else Conditions javascript

If Else Conditions javascript

  • "if else" conditions in javascript are used execute the code based on the condition. That's why these also called as conditional statements.

If statement

Use Case: If purchase amount is more that 1000 then give customer a free chocolate.

let purchaseAmount = 1500;
if (purchaseAmount > 1000){
    console.log("You won a free chocolate");
}

If...else statement

Use case: Check if user is logged in or not.

let isLoggeIn = false;
if (isLoggeIn){
    console.log("Welcome User!");
}else{
    console.log("Please login");
}

If...else if..else statement

Use Case: Age Appropriate Drinks

if (age < 14) {
    return "drink fruit juice";
} else if (age < 18) {
    return "drink soda";
} else if (age < 21) {
    return "drink fruit-flavored beer";
} else {
    return "drink throat-piercing vodka";
}