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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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);
}
}
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