Line At A Time
Lesson 18: Counting Loops
Counting Loops Loop a set number of times with a counter.
1for (let i = 0; i < 5; i++) {
2 console.log(i);
3}
Terminal
$ node main.js
0
1
2
3
4
Every line, explained
for (let i = 0; i < 5; i++) {
The classic counting loop has three parts separated by semicolons: let i = 0 creates the counter (let, because it changes!), i < 5 keeps looping while true, and i++ adds 1 to i after every trip.
console.log(i);
console.log(...) prints whatever is inside the round brackets to the terminal, then moves to a new line. console is the terminal itself, log is the tool that writes to it, joined with a dot. Runs five times, printing the current count. Notice it starts at 0, just like array indexes.
}
This } closes the for loop.