Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

JS
JavaScript
0/12 completed
Beginner
What is JavaScript?Variables and data typesOperators and expressionsMaking decisions: if and switchLoops
Intermediate
FunctionsArrays and array methodsObjects
Advanced
Scope and closuresClasses and OOPPromises and async/await
Pro
The event loop
Tracks / JavaScript / Beginner

Loops

A loop repeats code. Instead of writing console.log 100 times, you write it once and loop 100 times. Loops are how programs process every item in a list, every row in a file, or every user in a database.

The for loop

JavaScript · editable

A for loop has three parts, separated by ;:

  1. Start: let i = 1 runs once, before the loop begins.
  2. Condition: i <= 5 is checked before every lap. The loop stops when it's false.
  3. Step: i++ runs after every lap.

Looping over a list: for...of

This is the most common loop in modern JavaScript.

JavaScript · editable

The while loop

Use while when you don't know in advance how many laps you need.

JavaScript · editable

Infinite loops: if the condition never becomes false, the loop runs forever. This site stops your code after a few seconds, but in a real program it would freeze. Always make sure something inside the loop moves it toward the end.

break and continue

  • break exits the loop completely.
  • continue skips to the next lap.
JavaScript · editable

Building results with loops

A very common pattern: start with an empty value and build it up.

JavaScript · editable

Nested loops

JavaScript · editable

Practice: FizzBuzz

The classic interview warm-up. Print the numbers 1 to 20, but print Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both.

JavaScript · editable
✎ Check your understanding

1.How many times does for (let i = 0; i < 3; i++) run?

2.Which loop is best for going through every item in an array?

← Making decisions: if and switch