Lesson 11 ยท about 15 minutes

๐Ÿง  Variables: Memory Boxes

Welcome to the JavaScript Deep Dive! In lesson 8 you wired a button. Today you learn how JavaScript REMEMBERS things: little labeled boxes in the computer's brain. Prices, names, how many sneakers are left โ€” all live in boxes.

๐ŸŽฌ Watch first (90 seconds)

Boxes with labels

Imagine a shelf full of boxes. Each box has a label on the front and one thing inside. That's exactly what a variable is: a labeled box in the computer's memory.

const price = 5;
let count = 0;

Read it like a kid, not like a robot:

Rule of thumb: use const almost always. Only use let when you KNOW the box needs refilling โ€” like a score, or how many items are left in stock.

Two kinds of stuff (so far)

For now, boxes hold two kinds of things:

Numbers can do math with + - * / (yes, * means times and / means divide). And + has a second job: it glues strings together:

const price = 5;
const sentence = "Price: " + price + " dollars";
// the box "sentence" now holds: Price: 5 dollars
๐Ÿ’ก The one idea of this lesson The + sign checks for quotes before it works:
5 + 5      โ†’  10      (two numbers: MATH)
"5" + 5    โ†’  "55"    (a string is there: GLUE!)
Whoa. "5" + 5 is "55", not 10! Why? Because "5" is wearing quotes, so it's text, not a number. And when text meets +, JavaScript stops doing math and starts gluing. The quotes decide everything: quotes = words, no quotes = math. Half the weird bugs in real shops on the internet come from this one trap โ€” and now YOU know it.
๐Ÿ”Œ NEW superpower: the preview box now has electricity! Big news: from this lesson on, the "Try it right here" preview RUNS your <script>! Your code runs live, again and again, on every single keystroke. That means your sentence will appear in the preview as you type โ€” magic! One rule: never use alert() in the preview, or it will pop up on every letter you type and drive you bananas.

๐Ÿ› ๏ธ Build it (the fun part)

๐Ÿงช Your mission Make your shop write its own price tag. You'll store the product name, the price, and the stock in memory boxes โ€” then glue them into one sentence on the page.
  1. Open practice/myshop.html in VS Code.
  2. Inside the <body>, add an empty sign for the sentence:
    <p id="info"></p>
  3. Inside your <script> at the end of the body (or make a new one there), add three memory boxes (type them, don't copy-paste!):
    const productName = "Robo Sneakers";
    const price = 5;
    let stock = 10;
    Name and price are sealed with const. Stock uses let because it will go down when people buy!
  4. Right under the boxes, glue everything into the sign:
    document.getElementById("info").textContent =
      productName + " costs " + price + " dollars โ€” " + stock + " left!";
  5. Save (โŒ˜S), open myshop.html in the browser, refresh (โŒ˜R). You should see: Robo Sneakers costs 5 dollars โ€” 10 left!
  6. Now the cool part: change price to 9, save, refresh. The sentence updates itself! You changed ONE box and the page rewrote its own words. That's why coders love variables. ๐Ÿง ๐ŸŽ‰

๐Ÿ•น๏ธ Try it right here โ€” the lesson checks your code!

Remember: the preview now RUNS your script live. Watch your sentence appear while you type, then press Check my code โœ”.

๐Ÿ”จ Now break it!

Do these in your real myshop.html file, in the real browser. Predict what will happen BEFORE you refresh!

  1. Wrap a number in quotes. Change const price = 5; to const price = "5"; โ€” then add + price twice somewhere in your sentence, like price + price. Save, refresh. Math just turned into gluing: "55"! Take the quotes off and it's 10 again. Quotes decide: words or math.
  2. Break a label. Rename price to prise in ONE place only (the box line, not the sentence). Save, refresh โ€” the sentence is GONE. Right-click โ†’ Inspect โ†’ Console: a red error saying price is not defined. The browser looked for a box labeled price and found nothing. Fix it back.
  3. Do shop math. Try price * 2 in your sentence (price for TWO pairs), or 100 / price (how many pairs 100 dollars buys). * is times, / is divide โ€” your shop can now calculate!
โš ๏ธ If your sentence doesn't show up Check three things: does the id in getElementById("info") match your <p id="info"> EXACTLY? Is every string wearing BOTH its quotes? And peek in the Console (right-click โ†’ Inspect) โ€” one red line up top usually names the exact problem and even the line number.

๐ŸŽฎ Quiz time

No peeking at the notes above โ€” trying to remember is what makes it stick!

๐Ÿ“š Want more?

โญ Best thing to read next MDN: Storing the information you need โ€” Variables โ€” the official guide from Mozilla (the Firefox people). It goes a little deeper into boxes and what fits inside them. Reading it counts as a lesson block! And the JS Cheat Sheet now needs a new corner: write const vs let on it yourself.