Javascript Conditional  Statements with Some Examples

Javascript Conditional Statements with Some Examples

  1. Driving License Eligibility Check:
let age = 18;
let testPassed = true;

if (age >= 18) {
    if (testPassed) {
        console.log("Congratulations! You are eligible and have obtained the license.");
    } else {
        console.log("Sorry, you did not clear the test.");
    }
} else {
    console.log("Not eligible for the license.");
}
  1. Leap Year Check:
function isLeapYear(year) {
    return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
  1. Traffic Light Simulation using Switch:
let color = "red";

switch (color) {
    case "red":
        console.log('STOP! The light is red.');
        break;
    case "yellow":
        console.log('CAUTION! The light is yellow.');
        break;
    case "green":
        console.log('GO AHEAD!');
        break;
    default:
        console.log('Invalid Color');
}
  1. Coffee Machine Order Handling with Switch:
function coffeeMachine(coffeeType) {
    let answer;

    coffeeType = coffeeType.toLowerCase();

    switch (coffeeType) {
        case "regular":
        case "espresso":
            answer = "$2.50";
            break;
        case "latte":
            answer = "$3.50";
            break;
        case "cappuccino":
            answer = "$4.00";
            break;
        default:
            answer = "Invalid coffee type";
            break;
    }

    return answer;
}