Nearby lessons
16 of 40🔄 JavaScript Loops
📌 What are JavaScript Loops?
Loops in JavaScript are used to execute the same block of code repeatedly. 💫
They help automate repetitive tasks and process collections of data efficiently.
📋 Types of Loops
- 🔂 for - loops through a block of code a number of times
- 🔄 while - loops through code while a condition is true
- ⏮️ do...while - similar to while but runs at least once
- 🏷️ for...in - loops through properties of an object
- 📋 for...of - loops through values of iterable objects
- 🗑️ forEach() - executes a function for each array element
🔂 for Loop
| Attribute | Description |
|---|---|
Syntax | for (initialization; condition; increment) |
Best for | When you know how many times to loop |
Example | Counting, array iteration |
🔄 while Loop
| Attribute | Description |
|---|---|
Syntax | while (condition) |
Best for | When iterations are unknown |
Example | Reading streams, game loops |
⏮️ do...while Loop
| Attribute | Description |
|---|---|
Syntax | do { ... } while (condition) |
Best for | When code must run at least once |
Example | Menu systems, input validation |
🏷️ for...in Loop
| Attribute | Description |
|---|---|
Syntax | for (key in object) |
Best for | Iterating over object properties |
Example | Inspecting objects, debugging |
📋 for...of Loop
| Attribute | Description |
|---|---|
Syntax | for (value of iterable) |
Best for | Iterating over arrays, strings, etc. |
Example | Processing collections |
🗑️ forEach() Method
| Attribute | Description |
|---|---|
Syntax | array.forEach(callback) |
Best for | Functional array processing |
Example | Transforming array data |
🚦 Loop Control Statements
| Attribute | Description |
|---|---|
break | Exits the loop immediately |
continue | Skips current iteration |
label | Identifies loops for control |
🧠 Test Your Knowledge
5 QuestionsProgress: 0 / 5
Keep Going!JS - For Loop