A Loop Inside a Loop
Loops within loops.
A Loop Inside a Loop is a free Javascript for Kids lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Javascript for Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Loop in a Loop?! 🤯
What if you put a loop INSIDE another loop? It is like a wheel spinning inside a bigger wheel! ⚙️⚙️
This is called a nested loop, and it makes amazing art!
Outer and Inner 🪆
Think of nesting dolls! 🪆 The outer loop is the big doll. The inner loop is the small doll inside.
For every ONE turn of the outer, the inner loop runs ALL its turns!
The First Nested Loop 🔁
Here the outer loop runs 2 times. Each time, the inner loop runs 3 times! Watch carefully.
for (let i = 1; i <= 2; i++) {
for (let j = 1; j <= 3; j++) {
console.log('i=' + i + ' j=' + j)
}
}Two Counters! ✌️
See? We have TWO counters now: i for the outer loop and j for the inner.
The inner j goes 1, 2, 3 every time the outer i changes!
for (let i = 1; i <= 2; i++) {
console.log('Outer ' + i + ' starts')
for (let j = 1; j <= 2; j++) {
console.log(' inner ' + j)
}
}How Many Times Total? 🔢
If the outer runs 3 times and inner runs 4 times, that is 3 × 4 = 12 total runs!
Let us count them.
let count = 0
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 4; j++) {
count = count + 1
}
}
console.log('Total runs: ' + count)Days and Meals 🍽️
Real example! For each DAY, we eat 3 MEALS. The outer loop is days, the inner is meals.
for (let day = 1; day <= 2; day++) {
console.log('Day ' + day)
for (let meal = 1; meal <= 3; meal++) {
console.log(' meal ' + meal)
}
}Stars in Rows ⭐
The outer loop makes ROWS. The inner loop prints stars in that row. Sneak peek of art!
for (let row = 1; row <= 3; row++) {
for (let s = 1; s <= 2; s++) {
console.log('star')
}
console.log('--- end of row ---')
}Order Matters 📋
The inner loop ALWAYS finishes all its turns before the outer loop moves on. Like reading every word in a line before going to the next line! 📖
for (let i = 1; i <= 2; i++) {
for (let j = 1; j <= 2; j++) {
console.log(i + ',' + j)
}
}Multiplying Numbers ✖️
Nested loops are great for math! Let us multiply i and j together.
for (let i = 1; i <= 2; i++) {
for (let j = 1; j <= 3; j++) {
console.log(i + ' x ' + j + ' = ' + (i * j))
}
}Pick Outfits 👕👖
2 shirts and 2 pants make 4 outfits! Nested loops show every combo.
for (let shirt = 1; shirt <= 2; shirt++) {
for (let pants = 1; pants <= 2; pants++) {
console.log('Shirt ' + shirt + ' with pants ' + pants)
}
}Your Turn to Nest 🌟
Change the inner loop to run 4 times and run it! See how the output grows. 🪆
for (let i = 1; i <= 2; i++) {
for (let j = 1; j <= 4; j++) {
console.log('i=' + i + ' j=' + j)
}
}Quick Check ✅
Nested loop brain check! 🧠
Nested Loop Recap 🎉
Mind blown! 🤯 You learned nested loops.
- A loop INSIDE a loop, like nesting dolls 🪆
- Outer counter
i, inner counterj - Inner finishes fully before outer moves on
Next: making rows and columns like a grid! 🟦
Frequently asked questions
Is the “A Loop Inside a Loop” lesson free?
Yes — the full text of “A Loop Inside a Loop” is free to read here on the web, and the Javascript for Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Javascript for Kids course, upgrade to CoddyKit PRO.
What will I learn in “A Loop Inside a Loop”?
Loops within loops. You practise Javascript for Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Javascript for Kids?
No prior experience is required. Javascript for Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “A Loop Inside a Loop” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Javascript for Kids lesson?
Yes. Every Javascript for Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- A Loop Inside a Loop
- Rows and Columns
- Star Triangles
- A Times Table