Lesson 12 ยท JavaScript Deep Dive ยท about 15 minutes
๐ฆ Decisions: if / else
Until now your code always did the SAME thing. Today it learns to choose: green light โ go this way, red light โ go that way. Your shop is about to get smart!
๐ฌ Watch first (90 seconds)
Code at the traffic light
Imagine your code driving down the street. It reaches a traffic light and asks a question: "Is the light green?"
- YES โ drive on! ๐ข
- NO โ stop and do something else. ๐ด
In JavaScript, that traffic light is called if / else. It looks like this:
if (question) {
// do this when the answer is YES
} else {
// do that when the answer is NO
}
The question goes in the round brackets ( ). To ask questions, you use comparers โ tiny math-looking symbols:
===โ "are these two things exactly equal?">โ "is the left one bigger?"<โ "is the left one smaller?"
So if (stock > 0) means: "Hey browser, is the number in the stock box bigger than 0? If yes, do the stuff in the curly brackets."
stock = 3 // = PUTS the number 3 into the box (changes it!)
stock === 3 // === ASKS "does the box hold exactly 3?" (just looks!)
Remember your memory boxes from lesson 11? = is the hand that stuffs something into a box. === is the detective who peeks inside and asks a question without touching anything. Mixing them up inside an if is the most famous bug in all of JavaScript โ you will hunt it down yourself in Task B!
Want MORE than two paths? Chain lights together with else if:
if (stock > 1) {
// lots left
} else if (stock === 1) {
// exactly one left โ "LAST ONE!"
} else {
// zero left โ sold out
}
The browser checks the questions from top to bottom and runs the FIRST one that answers YES. Like walking past traffic lights until one turns green.
๐ ๏ธ Build it (the fun part)
- Open
practice/myshop.htmlin VS Code. Find your Buy button script from lesson 8 (the one withaddEventListener). - Just above the
addEventListenerline, create a memory box for your stock (type it, don't copy-paste!):
We uselet stock = 3;let(notconst) because this number is going to CHANGE โ you're going to sell things! - Now replace what happens inside the click function with a traffic light:
Read it out loud: "When clicked: IF stock is bigger than zero, take one out of the box and celebrate. ELSE, sad trombone."btn.addEventListener("click", function () { if (stock > 0) { stock = stock - 1; document.getElementById("message").textContent = "๐ Bought! " + stock + " left"; } else { document.getElementById("message").textContent = "๐ข Sold out!"; } }); - Save (โS), open
myshop.htmlin your browser, refresh (โR). - Now click Buy now exactly 4 times and watch the message: 2 left... 1 left... 0 left... ๐ข Sold out! Your page just made FOUR different decisions with one button. ๐ฆ๐
๐น๏ธ Try it right here โ the lesson checks your code!
Big news: unlike lesson 8, the preview boxes below have the electricity turned ON โ your buttons really work when you click them! One rule: the preview re-runs your code on every letter you type, so never use alert() in there (it would pop up nonstop and drive you bananas ๐).
๐จ Now break it!
Do these in your real myshop.html file, in the real browser. Predict what will happen BEFORE you refresh!
- Cause the famous bug on purpose. Change
if (stock > 0)toif (stock = 0)โ one equals sign. Save, refresh, click. Chaos! The=doesn't ASK about the box, it stuffs 0 into it โ your whole stock vanishes in one click. Feel the danger... then put> 0back. - Flip the arrow. Change
stock > 0tostock < 0. Now the question is "is stock SMALLER than zero?" โ which is never yes. Click away: instant "Sold out!", even with 3 in the box. A comparer pointing the wrong way is a super common bug. Flip it back! - Add a third traffic light. Between the
ifand theelse, squeeze in:
Wait โ think first: does this line run when 1 is left, or when you buy the last one? Test it and find out. (Careful: three equals signs!)} else if (stock === 1) { document.getElementById("message").textContent = "๐ฅ LAST ONE! Grab it!";
if needs round brackets ( ) for the question AND curly brackets { } for the action. else gets NO question โ it's just "otherwise". And count your equals signs: one = changes, three === checks. The Console (right-click โ Inspect โ Console) will point at the exact line.
๐ฎ Quiz time
No peeking at the notes above โ trying to remember is what makes it stick!
๐ Want more?
if / else if / else โ keep it next to you.