Skip to content

Latest commit

 

History

History
163 lines (128 loc) · 4.44 KB

How-loops-work-in-JS.md

File metadata and controls

163 lines (128 loc) · 4.44 KB

How Loops Work in JavaScript ?

Loops in JavaScript are used to repeatedly execute a block of code until a specified condition is met. They are essential for handling repetitive tasks efficiently. JavaScript provides several types of loops, each suitable for different scenarios.


1. for Loop

The for loop is ideal when the number of iterations is known beforehand.

Syntax:

for (initialization; condition; increment/decrement) {
  // Code to execute
}
  • Initialization: Initializes a counter variable.
  • Condition: Determines how long the loop runs.
  • Increment/Decrement: Updates the counter variable after each iteration.

Example:

for (let i = 0; i < 5; i++) {
  console.log(i);
}
// Output: 0, 1, 2, 3, 4

2. while Loop

The while loop executes the code block as long as the specified condition evaluates to true.

Syntax:

while (condition) {
  // Code to execute
}

Example:

let i = 0;
while (i < 5) {
  console.log(i);
  i++;
}
// Output: 0, 1, 2, 3, 4

3. do...while Loop

This loop guarantees at least one execution of the code block, even if the condition is false initially.

Syntax:

do {
  // Code to execute
} while (condition);

Example:

let i = 0;
do {
  console.log(i);
  i++;
} while (i < 5);
// Output: 0, 1, 2, 3, 4

4. for...of Loop

This loop is designed for iterating over iterable objects like arrays, strings, Maps, or Sets.

Syntax:

for (variable of iterable) {
  // Code to execute
}

Example:

let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
  console.log(fruit);
}
// Output: apple, banana, cherry

5. for...in Loop

The for...in loop iterates over the enumerable properties of an object. It’s often used for objects rather than arrays.

Syntax:

for (variable in object) {
  // Code to execute
}

Example:

let car = { brand: "Tesla", model: "Model 3", year: 2023 };
for (let key in car) {
  console.log(`${key}: ${car[key]}`);
}
// Output:
// brand: Tesla
// model: Model 3
// year: 2023

Key Differences Between Loop Types

Loop Type Use Case Example
for When the number of iterations is known in advance. Iterating over a range of numbers.
while When the condition depends on dynamic factors or external input. Waiting for user input.
do...while When you need to execute a block at least once before checking the condition. Menu-based applications.
for...of When working with iterables (arrays, strings, etc.). Iterating over an array.
for...in When iterating over the properties of an object. Reading object properties.

Best Practices

  1. Use for loops when dealing with numerical ranges or index-based operations.
  2. Use for...of loops for simpler syntax when iterating over arrays or other iterables.
  3. Avoid for...in loops for arrays as they might include unintended inherited properties.
  4. Always include a termination condition to prevent infinite loops.

Common Pitfalls

  1. Infinite Loops: Forgetting to update the loop variable or write the exit condition correctly.
    let i = 0;
    while (true) { // Infinite loop
      console.log(i);
    }
  2. Using for...in on Arrays:
    let arr = [1, 2, 3];
    for (let index in arr) {
      console.log(index); // Logs the indices as strings, not the values.
    }

Conclusion

Loops are versatile constructs in JavaScript that help automate repetitive tasks. By understanding the differences and applications of each type, you can select the right loop for your specific use case and write efficient, readable code.