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?"

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:

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."

๐Ÿ’ก The one idea of this lesson (and the #1 kid trap!) One equals sign CHANGES. Three equals signs CHECKS.
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)

๐Ÿงช Your mission Give your Buy button a real brain: it counts how many products are left, and when they're gone... it says so. A shop that can SELL OUT!
  1. Open practice/myshop.html in VS Code. Find your Buy button script from lesson 8 (the one with addEventListener).
  2. Just above the addEventListener line, create a memory box for your stock (type it, don't copy-paste!):
    let stock = 3;
    We use let (not const) because this number is going to CHANGE โ€” you're going to sell things!
  3. Now replace what happens inside the click function with a traffic light:
    btn.addEventListener("click", function () {
      if (stock > 0) {
        stock = stock - 1;
        document.getElementById("message").textContent =
          "๐ŸŽ‰ Bought! " + stock + " left";
      } else {
        document.getElementById("message").textContent = "๐Ÿ˜ข Sold out!";
      }
    });
    Read it out loud: "When clicked: IF stock is bigger than zero, take one out of the box and celebrate. ELSE, sad trombone."
  4. Save (โŒ˜S), open myshop.html in your browser, refresh (โŒ˜R).
  5. 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!

  1. Cause the famous bug on purpose. Change if (stock > 0) to if (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 > 0 back.
  2. Flip the arrow. Change stock > 0 to stock < 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!
  3. Add a third traffic light. Between the if and the else, squeeze in:
    } else if (stock === 1) {
      document.getElementById("message").textContent = "๐Ÿ”ฅ LAST ONE! Grab it!";
    
    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!)
โš ๏ธ If the browser yells or nothing happens Check the usual suspects: every 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?

โญ Best thing to read next MDN: Making decisions in your code โ€” conditionals โ€” the official guide from Mozilla (the Firefox people), full of extra examples like a weather picker. Reading it counts as a lesson block! And the JS Cheat Sheet now has a spot for if / else if / else โ€” keep it next to you.