Loops
The Problem Loops Solve
Suppose you want to print the numbers 1 through 10. Without loops:
console.log(1);
console.log(2);
console.log(3);
// ... seven more lines
console.log(10);
Now suppose you want 1 through 10,000. That's not a program — that's punishment. The computer can do repetitive work at billions of operations per second. Your job is to describe the repetition once, then let the machine do it.
The for Loop
The most common loop in JavaScript:
The structure has three parts inside the parentheses, separated by semicolons:
for (initialisation; condition; update) {
// body
}
- Initialisation —
let i = 1— runs once before the loop starts; creates the counter - Condition —
i <= 10— checked before every iteration; if false, loop stops - Update —
i++— runs after every iteration;i++meansi = i + 1
Read it as: "Start with i=1. While i is ≤ 10, run the body, then increment i."
The variable i is just a convention — you can name it anything. i stands for index, and you will see it everywhere.
Counting Down, Stepping by 2
The update step can be anything: i--, i += 2, i *= 2. The loop is fully general.
The while Loop
while is simpler: it repeats as long as a condition is true.
for and while can always replace each other. The convention is:
- Use
forwhen you know in advance how many times to loop - Use
whilewhen you loop until something happens — and you don't know how many steps that will take
You don't know upfront how many doublings it takes to exceed 1000. while is the natural fit.
break and continue
Two keywords that give you fine control inside any loop:
break is an emergency exit — the loop ends entirely. continue is a skip — the current iteration is abandoned and the loop moves on to the next one.
Loops + Functions = Power
Loops become genuinely powerful when combined with functions from the previous lesson:
The prime check runs up to √n rather than n — a classical optimisation. If n has no factor up to its square root, it has no factors at all. The outer loop just calls the function for each candidate.
Arrays — A First Look
A loop that counts numbers is useful. A loop that processes a collection of real data is essential. That means we need arrays — and arrays get their own full lesson next. But you need a preview now so the loop examples feel real.
An array is an ordered list of values:
temperatures[i] accesses the element at position i. Arrays are zero-indexed — position 0 is the first element, position length - 1 is the last. The loop runs from 0 to length - 1 exactly.
The for...of Loop
The index-based for loop is powerful but verbose when you just want every element. for...of is cleaner:
for...of gives you each value directly, without indices. Use it when you don't need to know the position — which is most of the time.
forEach — The Functional Way
Because functions are values (Lesson 5), arrays have a built-in method that accepts a function and calls it once for each element:
forEach takes an arrow function. That function receives each element (and optionally its index) as arguments. This is your first real look at higher-order functions — functions that take other functions as arguments. Arrays are full of them, and they are the subject of the lesson after next.
A Complete Example: Statistics
One function, any dataset. The loop does the traversal; the function wraps the logic; the result is readable, reusable, and correct. This is what programs look like when they grow up.
What You Have Learned
Loops are how you tell the computer to repeat work without repeating yourself.
The key ideas:
for— best when you know the count: initialise, condition, updatewhile— best when you loop until something happensbreakexits a loop entirely;continueskips one iterationfor...of— the clean way to visit every element of an arrayforEach— the functional way; passes each element to a callback function- Loops + functions = the first real taste of computational power
In the next lesson: Arrays in full — how to store, access, transform, search, and sort collections of data. Arrays are the most-used data structure in JavaScript, and their built-in methods (map, filter, reduce) will change how you think about processing information.