☕ The Robot Barista: Mastering Decisions in JavaScript
Code is usually like a recipe: Step 1, Step 2, Step 3. But what if Step 2 depends on something? What if you only add sugar if the customer asks for it? This is called Control Flow (or Decision Making). They act like forks in the road where the code has to choose which path to take.
Today, we are going to build a Virtual Barista that decides what drink to make based on what the customer says!
1. The Simple Choice (if)
The if statement is the most basic question. It only runs code if the answer is YES (true).
Scenario: The Robot checks if the coffee machine is turned on.
let isMachineOn = true;
// 1. SIMPLE IF
if (isMachineOn) {
console.log("✅ Welcome! The café is OPEN.");
}Explanation: The robot looks at isMachineOn. If it is true, it greets you. If it were false, the robot would stay silent.
2. The “This or That” Choice (if-else)
Sometimes we need a backup plan. If the answer is NO, we need to do something else.
Scenario: The Robot checks if the customer has enough money.
let cash = 3;
let price = 5;
// 2. IF-ELSE
if (cash >= price) {
console.log("☕ Here is your coffee!");
} else {
console.log("❌ Sorry, you need more money.");
}Explanation:
- IF you have enough cash ($5 or more), you get coffee.
- ELSE (otherwise), you get rejected. The robot must choose one of these two paths.
3. The Multiple Choice (else if)
What if there are more than two options? We can chain them together like a ladder.
Get Lydia Jennifer’s stories in your inbox
Join Medium for free to get updates from this writer.
Scenario: The customer chooses a cup size.
let size = "Large";
// 3. ELSE-IF LADDER
if (size === "Small") {
console.log("Here is a cute 8oz cup. 🥤");
} else if (size === "Medium") {
console.log("Here is a standard 12oz cup. 🥤");
} else if (size === "Large") {
console.log("Here is a giant 16oz cup! 🥤");
} else {
console.log("I don't know that size!");
}Explanation: The robot checks the first condition. If it’s false, it drops to the next else if. It keeps checking until it finds a match.
4. The Menu Board (switch)
When you have a lot of specific options (like a menu), else if gets messy. The switch statement is cleaner. It's like a vending machine: you press a button, and that specific item drops.
Scenario: Selecting the type of drink.
let drinkType = "Latte";
// 4. SWITCH STATEMENT
switch (drinkType) {
case "Espresso":
console.log("Strong and black! ☕");
break; // STOP here! Don't check the others.
case "Latte":
console.log("Milky and smooth! 🥛");
break;
case "Mocha":
console.log("Chocolate goodness! 🍫");
break;
default:
// This runs if the drink isn't on the menu
console.log("We don't serve that here.");
}Explanation:
case: The robot jumps directly to the matching word ("Latte").break: This is crucial! It tells the robot "I'm done, stop reading the menu."default: This is the catch-all (like theelse) for when nothing matches.
🚀 The Grand Finale: Real-Time Example
Let’s put it all together into one smart script. Copy this into your console!
let isMachineOn = true;
let money = 10;
let drinkChoice = "Latte";
// Step 1: Is the shop open?
if (isMachineOn) {
// Step 2: Can they afford it? (Nested If!)
if (money >= 5) {
// Step 3: Make the drink
switch (drinkChoice) {
case "Latte":
console.log("Here is your creamy Latte!");
break;
case "Espresso":
console.log("Here is your strong Espresso!");
break;
default:
console.log("Just water for you.");
}
} else {
console.log("Not enough money!");
}
} else {
console.log("Come back tomorrow!");
}Now you have a robot that checks the power, checks your wallet, and takes your order. You’ve mastered decision making! 🤖☕










