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:
const price = 5;โ "Make a box, glue the labelpriceon it, put a5inside, and seal it shut forever."constboxes can NEVER be refilled.let count = 0;โ "Make a box labeledcountwith a0inside, but leave the lid open."letboxes can be refilled later:count = 3;
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 โ just write them:
5,100,3.5. No quotes! - Strings โ that's coder-speak for text. Always in quotes:
"hello","Robo Sneakers". Think of the quotes as the gift wrap around words.
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
+ 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.
alert() in the preview, or it will pop up on every letter you type and drive you bananas.
๐ ๏ธ Build it (the fun part)
- Open
practice/myshop.htmlin VS Code. - Inside the
<body>, add an empty sign for the sentence:<p id="info"></p> - 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!):
Name and price are sealed withconst productName = "Robo Sneakers"; const price = 5; let stock = 10;const. Stock usesletbecause it will go down when people buy! - Right under the boxes, glue everything into the sign:
document.getElementById("info").textContent = productName + " costs " + price + " dollars โ " + stock + " left!"; - Save (โS), open
myshop.htmlin the browser, refresh (โR). You should see: Robo Sneakers costs 5 dollars โ 10 left! - Now the cool part: change
priceto9, 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!
- Wrap a number in quotes. Change
const price = 5;toconst price = "5";โ then add+ pricetwice somewhere in your sentence, likeprice + price. Save, refresh. Math just turned into gluing:"55"! Take the quotes off and it's 10 again. Quotes decide: words or math. - Break a label. Rename
pricetoprisein 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 labeledpriceand found nothing. Fix it back. - Do shop math. Try
price * 2in your sentence (price for TWO pairs), or100 / price(how many pairs 100 dollars buys).*is times,/is divide โ your shop can now calculate!
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?
const vs let on it yourself.