|
499 | 499 | | 476 | [Why is it important to remove event listeners after use?](#why-is-it-important-to-remove-event-listeners-after-use) | |
500 | 500 | | 477 | [What is structuredClone and how is it used for deep copying objects?](#what-is-structuredclone-and-how-is-it-used-for-deep-copying-objects) | |
501 | 501 | | 478 | [What is the difference between const and Object.freeze](#what-is-the-difference-between-const-and-objectfreeze) | |
| 502 | +| 479 | [What is the difference between for loop and forEach](#what-is-the-difference-between-for-loop-and-foreach) | |
502 | 503 | <!-- TOC_END --> |
503 | 504 |
|
504 | 505 | <!-- QUESTIONS_START --> |
@@ -9577,6 +9578,59 @@ Common use cases and benefits: |
9577 | 9578 | **[⬆ Back to Top](#table-of-contents)** |
9578 | 9579 |
|
9579 | 9580 |
|
| 9581 | +479. ### What is the difference between for loop and forEach |
| 9582 | +
|
| 9583 | + Both are used to iterate over arrays, but they differ in performance, flexibility, and purpose. |
| 9584 | +
|
| 9585 | + The `for` loop is a core JavaScript statement that gives you full control over iteration: |
| 9586 | + ```javascript |
| 9587 | + const arr = [1, 2, 3]; |
| 9588 | + for (let i = 0; i < arr.length; i++) { |
| 9589 | + console.log(arr[i]); |
| 9590 | + } |
| 9591 | + ``` |
| 9592 | +
|
| 9593 | + The `forEach` is an array method introduced in ES5 that accepts a callback and calls it for each element: |
| 9594 | +
|
| 9595 | + ```javascript |
| 9596 | + const arr = [1, 2, 3]; |
| 9597 | + arr.forEach((num) => console.log(num)); |
| 9598 | + ``` |
| 9599 | +
|
| 9600 | + **Why is `for` loop faster?** |
| 9601 | +
|
| 9602 | + `forEach` has two sources of overhead on every iteration: first, it |
| 9603 | + invokes your callback using `.call()` (a function call has a cost). |
| 9604 | + Second, it checks for empty slots in the array (`i in this`) on every |
| 9605 | + single iteration even when the array has no empty slots at all. The |
| 9606 | + `for` loop does neither of these things, and JavaScript engines like |
| 9607 | + V8 are highly optimized for its simple counter pattern. |
| 9608 | +
|
| 9609 | + **Why was `forEach` created?** |
| 9610 | +
|
| 9611 | + For readability. The classic `for (let i = 0; i < arr.length; i++)` |
| 9612 | + is noisy you have to declare a counter, write a condition, and |
| 9613 | + increment manually, just to access each element. `forEach` hides all |
| 9614 | + of that mechanics and lets you focus on the element itself. Arrow |
| 9615 | + functions later made it even cleaner. However, they also made its |
| 9616 | + optional `thisArg` parameter (used to set `this` inside the callback) |
| 9617 | + mostly obsolete, since arrow functions inherit `this` automatically. |
| 9618 | +
|
| 9619 | + **When to use which:** |
| 9620 | + ```javascript |
| 9621 | + // Use forEach — simple iterations where readability matters |
| 9622 | + arr.forEach((num) => console.log(num)); |
| 9623 | +
|
| 9624 | + // Use for loop — when you need break, continue, or max performance |
| 9625 | + for (let i = 0; i < arr.length; i++) { |
| 9626 | + if (arr[i] === target) break; // impossible with forEach |
| 9627 | + } |
| 9628 | + ``` |
| 9629 | +
|
| 9630 | + **Note:** The `for` loop is strictly more powerful anything `forEach` does, `for` loop can do too, but not the other way around. |
| 9631 | +
|
| 9632 | +**[⬆ Back to Top](#table-of-contents)** |
| 9633 | +
|
9580 | 9634 | <!-- QUESTIONS_END --> |
9581 | 9635 | ### Coding Exercise |
9582 | 9636 |
|
|
0 commit comments