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

AttributeDescription
Syntaxfor (initialization; condition; increment)
Best forWhen you know how many times to loop
ExampleCounting, array iteration

🔄 while Loop

AttributeDescription
Syntaxwhile (condition)
Best forWhen iterations are unknown
ExampleReading streams, game loops

⏮️ do...while Loop

AttributeDescription
Syntaxdo { ... } while (condition)
Best forWhen code must run at least once
ExampleMenu systems, input validation

🏷️ for...in Loop

AttributeDescription
Syntaxfor (key in object)
Best forIterating over object properties
ExampleInspecting objects, debugging

📋 for...of Loop

AttributeDescription
Syntaxfor (value of iterable)
Best forIterating over arrays, strings, etc.
ExampleProcessing collections

🗑️ forEach() Method

AttributeDescription
Syntaxarray.forEach(callback)
Best forFunctional array processing
ExampleTransforming array data

🚦 Loop Control Statements

AttributeDescription
breakExits the loop immediately
continueSkips current iteration
labelIdentifies loops for control

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!JS - For Loop