JavaScript Loops for Beginners: For and While Loops
Learn JavaScript loops for beginners with simple, line-by-line examples of for, while, and do...while loops, plus how to loop through arrays safely.
Learn JavaScript loops for beginners with simple, line-by-line examples of for, while, and do...while loops, plus how to loop through arrays safely.
If you searched for "JavaScript loops for beginners," here is the short answer: a loop is a way to run the same block of code over and over, automatically, instead of writing it out by hand every time. JavaScript gives you a few kinds of loops, and the two you will use the most are the for loop and the while loop. This guide explains both, step by step, with small examples you can copy, paste, and run right now.
You do not need any prior experience to follow along. We will define every term the first time it appears, and every code example is short enough to type into your browser console in a few seconds.
A loop is a piece of code that repeats a set of instructions until a condition tells it to stop. Imagine you need to print the numbers 1 through 5. Without a loop, you would write five separate lines of code. With a loop, you write the instruction once, and JavaScript repeats it for you.
Loops matter because real programs deal with lists all the time: a list of products in a shop, a list of comments on a post, a list of rows from a database. Instead of writing code for each item by hand, you write one loop that handles every item, no matter how many there are.
The for loop is the most common loop in JavaScript. It is built for situations where you know how many times you want to repeat something, like "do this 5 times" or "do this once for every item in a list."
for (let i = 1; i <= 5; i++) {
console.log(i);
}Here is what each part does, in plain words:
So this loop prints 1, then 2, then 3, then 4, then 5, and then stops because 6 <= 5 is false.
A while loop repeats code as long as a condition stays true. Use it when you do not know in advance exactly how many times you need to repeat something.
let count = 1;
while (count <= 5) {
console.log(count);
count++;
}Line by line: let count = 1 creates a starting variable, this time outside the loop. while (count <= 5) checks the condition before every repeat, just like the for loop's condition. Inside the loop body, console.log(count) prints the current value, and count++ increases it by 1 so the loop eventually stops.
“If you forget to update the variable inside a while loop (here, count++), the condition never becomes false, and the loop runs forever. This is called an infinite loop, and it can freeze your browser tab.”
A do...while loop is almost identical to a while loop, with one difference: it runs the code first and checks the condition after. This guarantees the loop body runs at least once, even if the condition is false from the start.
let n = 10;
do {
console.log(n);
n++;
} while (n < 5);Even though n < 5 is false right away (10 is not less than 5), this loop still prints 10 once, because do...while always runs the body before it checks the condition. You will not use this loop often as a beginner, but it is good to recognize it when you see it.
An array is a list of values stored in one variable, like ["apple", "banana", "cherry"]. Looping through arrays is one of the most common reasons to use a loop. If you have not covered arrays yet, it helps to read our guide on JavaScript arrays for beginners before continuing.
const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
console.log(fruit);
}The for...of loop is a special, easier version of the for loop built for arrays and other lists. for (const fruit of fruits) means "for each value in fruits, call it fruit, and run the code below." You do not need to manage a counter variable like i — JavaScript handles that for you.
You can also loop through an array with a regular for loop and an index, which is useful when you need the position of each item:
const fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(i, fruits[i]);
}fruits.length is a property that tells you how many items are in the array (here, 3). fruits[i] reads the item at position i. Array positions start at 0, so fruits[0] is "apple", fruits[1] is "banana", and fruits[2] is "cherry".
New to arrays? Learn how they work before you loop through them.
Read the arrays guideSometimes you want to stop a loop early, or skip one item without stopping the whole loop. JavaScript gives you two keywords for this.
for (let i = 1; i <= 10; i++) {
if (i === 5) {
break; // stop the loop completely
}
console.log(i);
}This prints 1, 2, 3, 4, and then stops as soon as i equals 5, because break exits the loop immediately, even though the condition i <= 10 is still true.
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue; // skip this one number
}
console.log(i);
}continue skips just the current repeat and moves on to the next one. This prints 1, 2, 4, 5 — it skips printing 3, but the loop keeps going afterward.
| Loop type | Best for | Example use case |
|---|---|---|
| for | Repeating a known number of times | Print numbers 1 to 10 |
| while | Repeating until a condition changes, unknown count | Keep asking for input until it's valid |
| do...while | Code that must run at least once | Show a menu, then check if the user wants to quit |
| for...of | Looping through array items directly | Print every product name in a list |
Open your browser, right-click anywhere on a page, choose "Inspect," and click the "Console" tab. Paste any of the code examples above and press Enter. Try changing the numbers or the condition and see how the output changes — this is the fastest way to build a real feel for how loops work.
A for loop is best when you know how many times you want to repeat something, because the counter, condition, and update step are all written on one line. A while loop is best when you don't know the exact number of repeats in advance and just need to keep going until a condition becomes false.
An infinite loop happens when the loop's condition never becomes false, so it runs forever and can freeze your browser tab. To avoid it, always make sure something inside the loop (like a counter variable) changes on every repeat, moving the condition closer to false.
Yes. JavaScript arrays have built-in methods like forEach, map, and filter that loop through items for you. They are worth learning once you're comfortable with basic for and while loops, since they often make code shorter and easier to read.
i++ is shorthand for i = i + 1. It increases the variable i by exactly 1 each time it runs. You'll see it at the end of almost every classic for loop, since it's what moves the counter forward and eventually stops the loop.
This is called zero-based indexing, and most programming languages use it, including JavaScript. The first item in an array is at position 0, the second at position 1, and so on. It takes a little practice, but it becomes automatic quickly.
Want a team that already knows JavaScript inside and out to build your product?
Talk to FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
What is an index in SQL? A plain-English guide with copy-paste examples showing how indexes speed up queries and when you actually need one.
Learn JavaScript DOM manipulation for beginners: select elements, change text and styles, and handle clicks with simple, copy-paste code examples.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.