Lesson 14 ยท JavaScript Deep Dive ยท about 20 minutes
๐ Lists & Loops
So far, one variable holds ONE thing. Today you get a variable that holds a whole TRAIN of things โ and a loop that does work for every wagon. Then your page starts drawing itself. Seriously.
๐ฌ Watch first (90 seconds)
An array is a train ๐
Imagine a little train where every wagon is a box with something inside. In JavaScript that train is called an array, and the whole train fits in ONE variable:
const features = ["fast", "cool", "cheap"];
Square brackets [ ] build the train, commas separate the wagons. Three strings, one variable. Nice and tidy.
Want to peek into one wagon? Use its number:
features[0] // "fast"
features[1] // "cool"
features[2] // "cheap"
0, not 1. So on a 3-wagon train the last wagon is features[2]. Ask for features[3] and you getโฆ undefined โ the empty ghost box. This mistake is so famous it has a name: the off-by-one bug. Every programmer on Earth has made it. Today you'll make it on purpose!
One more train trick: features.length tells you how many wagons there are. For our train that's 3. (Yes, it says 3 even though the last number is 2 โ because counting starts at 0.)
A loop repeats work for every wagon
What if you want to do something with EVERY wagon? You could write three linesโฆ but what about a train with 100 wagons? No way. You need a loop:
for (const f of features) {
console.log(f);
}
Read it out loud like English: "for each thing f of the features train, do this." The loop grabs "fast", runs the code, grabs "cool", runs it again, grabs "cheap", runs it once more, then stops. Three wagons, three runs โ automatically.
const features = ["fast", "cool", "cheap"];
let html = "";
for (const f of features) {
html = html + "<li>" + f + "</li>";
}
document.getElementById("list").innerHTML = html;
In kid language:
- Start with an empty string
htmlโ a blank piece of paper. - Each trip of the loop glues one more
<li>โฆ</li>onto the paper. innerHTMLis liketextContent's big sibling:textContentshows the words exactly as-is, butinnerHTMLreads the words as HTML bricks โ so<li>becomes a real list item, not the letters "<li>".
alert() in there โ it re-runs constantly and would drive you bananas.)
๐ ๏ธ Build it (the fun part)
<li> bricks right now. Rebuild it the pro way: put the features in an ARRAY and let a loop draw the list. Then grow the page by editing only the array. ๐คฏ
- Open
practice/myshop.htmlin VS Code and find your feature list (the<ul>with your product's 3 features). - Give the
<ul>an id and delete the hand-written<li>s inside it (brave, I know):<ul id="feature-list"></ul> - In your
<script>at the end of the body, build the train with YOUR three features (type them, don't copy-paste!):const features = ["super grippy", "glows in dark", "robot laces"]; - Now the loop that draws the list:
let html = ""; for (const f of features) { html = html + "<li>" + f + "</li>"; } document.getElementById("feature-list").innerHTML = html; - Save (โS) and refresh (โR). Your list looksโฆ exactly the same as before. Boring? NO โ wait for itโฆ
- Add a 4th feature to the array only โ just one string, one comma, nothing else. Save. Refresh. The page grew a new list item BY ITSELF. You didn't touch the HTML. The data changed, the loop did the rest. That's how every real shop site on the internet works. ๐
๐น๏ธ Try it right here โ the lesson checks your code!
These previews run your JavaScript for real. Type an extra string into your array and watch the list grow the moment you type the closing quote. Then press Check my code โ.
๐จ Now break it!
Do these in your real myshop.html file (or in the Task A box above). Predict what will happen BEFORE you refresh!
- Summon the ghost box. Somewhere in your script, add:
Wagon 99 doesn't exist, so the page literally prints the worddocument.getElementById("feature-list").innerHTML += "<li>" + features[99] + "</li>";undefined. That word on a webpage is a famous clue: somebody grabbed a box that isn't there. Now you know what it means when you see it in the wild! Remove the line after your laugh. - Double the train. Copy your whole
forloop and paste it right below itself (before theinnerHTMLline). Refresh โ every feature shows up TWICE. The loop doesn't know or care; it just runs for every wagon, every time you ask. Delete the copy. - Let the array brag about itself. Add a
<p id="count"></p>under your list, then in the script:
Now add a 5th feature to the array and refresh. The sentence updates its own number. The page counts FOR you.document.getElementById("count").textContent = "We have " + features.length + " amazing features!";
getElementById match the <ul>'s id EXACTLY? Is the script at the END of the body (after the <ul>)? And did you use innerHTML, not textContent? With textContent you'd see the raw letters <li>fast</li> on screen โ the bricks never get built.
๐ฎ Quiz time
No peeking at the notes above โ trying to remember is what makes it stick!
๐ Want more?
push (add a wagon from code!). Then read MDN: Loops for other kinds of loops. Reading them counts as a lesson block! And keep the JS Cheat Sheet next to you.