JS Loop For
In this page:
for (let i = start; i < end; i++) {
// loop body
}
for (const item of iterable) {
// loop body
}
The Standard for Loop
A standard for loop has three parts inside its parentheses, an initializer that runs once at the start, a condition checked before every iteration, and an increment step that runs after each iteration.
Example: The Standard for Loop
// Classic for-loop: `let i = 0; i < 3; i++`
// Classic for-loop: `let i = 0; i < 3; i++`
for (let i = 0; i < 3; i++) {
// Print `i` to the console
// Print `i` to the console
console.log(i);
}
for...in
for...in iterates over the enumerable property keys of an object, giving you each key name in turn, which you can then use to look up the corresponding value.
It's generally avoided for arrays since it iterates keys as strings and can pick up inherited properties.
Example: for...in
// Declare the constant `user`, set to `{ name: "Sam", age: 30 }`
// Declare the constant `user`, set to `{ name: "Sam", age: 30 }`
const user = { name: "Sam", age: 30 };
// Loop over the enumerable keys of `user`, binding each to `key`
// Loop over the enumerable keys of `user`, binding each to `key`
for (let key in user) {
// Print `key, user[key]` to the console
// Print `key, user[key]` to the console
console.log(key, user[key]);
}
for...of
for...of iterates directly over the values of any iterable, like arrays, strings, and more advanced structures like Sets and Maps, without needing to manage an index at all.
Example: for...of
// Declare the constant `colors`, set to `["red", "green", "blue"]`
// Declare the constant `colors`, set to `["red", "green", "blue"]`
const colors = ["red", "green", "blue"];
// Loop over `colors`, binding each item to `color`
// Loop over `colors`, binding each item to `color`
for (let color of colors) {
// Print `color` to the console
// Print `color` to the console
console.log(color);
}
break and continue
break immediately exits a loop entirely, skipping any remaining iterations, while continue skips only the current iteration and moves on to the next one, without ending the loop.
Example: break and continue
// Classic for-loop: `let i = 0; i < 5; i++`
// Classic for-loop: `let i = 0; i < 5; i++`
for (let i = 0; i < 5; i++) {
if (i === 3) break;
if (i === 1) continue;
// Print `i` to the console
// Print `i` to the console
console.log(i);
}
Nested Loops
A loop can be placed inside another loop, running the inner loop completely for every single iteration of the outer loop, commonly used for working with grid-like or two-dimensional data.
Example: Nested Loops
// Classic for-loop: `let i = 1; i <= 2; i++`
// Classic for-loop: `let i = 1; i <= 2; i++`
for (let i = 1; i <= 2; i++) {
// Classic for-loop: `let j = 1; j <= 2; j++`
// Classic for-loop: `let j = 1; j <= 2; j++`
for (let j = 1; j <= 2; j++) {
// Print `i, j` to the console
// Print `i, j` to the console
console.log(i, j);
}
}
- Writing an incorrect stopping condition, like using <= instead of < with a zero-based array length, causing an out-of-bounds access.
- Forgetting to increment the loop counter, which creates an infinite loop that never satisfies its stopping condition.
- Using for...in on an array when for...of or a regular for loop is actually the more appropriate, index-safe choice.
- A standard for loop runs based on an initializer, a condition, and an increment step, all defined in its parentheses.
- for...in iterates over an object's property keys, while for...of iterates directly over an iterable's values.
- break exits a loop entirely, continue skips to the next iteration, and loops can be nested inside each other.
for, for...in, and for...of loops are all core JavaScript features supported identically in every modern browser.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic