Skip to content

Commit bafca99

Browse files
Add question about the difference between for loop and forEach
1 parent 5de110a commit bafca99

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@
499499
| 476 | [Why is it important to remove event listeners after use?](#why-is-it-important-to-remove-event-listeners-after-use) |
500500
| 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) |
501501
| 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) |
502503
<!-- TOC_END -->
503504

504505
<!-- QUESTIONS_START -->
@@ -9577,6 +9578,59 @@ Common use cases and benefits:
95779578
**[⬆ Back to Top](#table-of-contents)**
95789579
95799580
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+
95809634
<!-- QUESTIONS_END -->
95819635
### Coding Exercise
95829636

0 commit comments

Comments
 (0)