JAVASCRIPT - Part 6
The Codex (from Beginner to Pro)
JAVASCRIPT - Part 6

The Codex (from Beginner to Pro)
Mastering Loops and Iteration in JavaScript
Loops are used in JavaScript to repeat a block of code multiple times instead of writing the same code again and again.
They help programmers:
- Repeat tasks automatically
- Work with arrays and objects
- Create timers and animations
- Process large amounts of data
for Loop
A for loop is used when you know how many times the loop should run.
Syntax:
for (initialization; condition; increment) {
// code
}
for (let i = 1; i <= 5; i++) {
console.log("Number: " + i);
}
Explanation
let i = 1→ starts the loopi <= 5→ loop runs while condition is truei++→ increasesiby 1 after every loop
while Loop
A while loop runs as long as the condition remains true.
Syntax
while (condition) {
// code
}
let count = 1;
while (count <= 3) {
console.log("Count is: " + count);
count++;
}
do...while Loop
This loop runs the code at least once before checking the condition.
Syntax
do {
// code
} while (condition);
let num = 1;
do {
console.log("Number: " + num);
num++;
} while (num <= 3);
Looping Through Arrays
Loops are commonly used with arrays.
let fruits = ["Apple", "Banana", "Orange"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Nested Loops
A loop inside another loop is called a nested loop.
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 2; j++) {
console.log("i = " + i + ", j = " + j);
}
}
Infinite Loop
An infinite loop keeps running forever because the condition never becomes false.
while (true) {
console.log("This loop never ends");
}
for (;;) {
console.log("Running forever...");
}
This works because:
- No condition is given
- JavaScript assumes the condition is always true
⚠️ User Warning:
Infinite loops can freeze your browser or computer if not controlled properly. You can exit an infinite loop by:
- Clicking Ctrl + C - if run on a terminal or console
- Restart PC - If the above one doesn’t stop the loop
Breaking a Loop
The break statement stops a loop immediately.
for (let i = 1; i <= 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
Skipping an Iteration
The continue statement skips the current loop iteration.
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
Real-Life Example of Loops
Imagine a teacher wants to print attendance for 50 students.
Without loops:
console.log("Student 1");
console.log("Student 2");
console.log("Student 3");
Using loops:
for (let i = 1; i <= 50; i++) {
console.log("Student " + i);
}
Much faster and cleaner.
Medium-Level Task
Student Score Checker
Write a program that:
- Stores 5 student scores in an array
- Uses a loop to display each score
- Shows whether each student passed or failed
- Stop the loop if the score is negative
Solution
let scores = [70, 45, 88, -1, 60];
for (let i = 0; i < scores.length; i++) {
// Stops the loop if the score is negative
if (scores[i] < 0) {
console.log("Invalid score detected");
break;
}
// Shows whether each student passed or failed
console.log("Score: " + scores[i]);
if (scores[i] >= 50) {
console.log("Passed");
} else {
console.log("Failed");
}
}
Assignment 1
Create a multiplication table from 1 to 12 using a for loop.
Assignment 2
Create an array of 6 names and:
- Display all names using a loop
- Count how many names have more than 5 letters
Conclusion
Loops are one of the most important concepts in JavaScript. They help automate repetitive tasks, work with arrays, and make programs more efficient. Understanding for, while, do...while, break, and continue will help you write cleaner and smarter code.
👉 Next up: Part 7- Mastering Modern ES6+ JavaScript Features
메타데이터
- post_id
- 7552b3e1e0ea
- slug
- javascript-part-6-7552b3e1e0ea
- url
- https://medium.com/@teejaymezue/javascript-part-6-7552b3e1e0ea
- canonical_url
- https://medium.com/@teejaymezue/javascript-part-6-7552b3e1e0ea
- author_url
- https://medium.com/@teejaymezue
- status
- ok
- fetched_at
- 2026-08-08 21:34:13