Javascript Conditional Statements with Some Examples

1 min read

- 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.");
}
- Leap Year Check:
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
- 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');
}
- 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;
}
1
Subscribe to my newsletter
Read articles from Abhishek Dandriyal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abhishek Dandriyal
Abhishek Dandriyal
Frontend Developer , I design and code user-friendly and responsive web applications and projects using,HTML5 , React /Redux MUI . I collaborate with backend developer, UI/UX designers, and project managers to deliver high-quality products that meet the clients' needs and expectations.