Skip to content

Commit bbd51bf

Browse files
committed
added why forEach loop not returning undefined
1 parent 7ed6644 commit bbd51bf

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8594,7 +8594,9 @@ The execution context is created when a function is called. The function's code
85948594
```javascript
85958595
const arr = [1, 2, 3, 4, 5];
85968596
arr.map(x => x * x); // [1, 4, 9, 16, 25]
8597-
arr.forEach(x => x * x); // undefined
8597+
arr.forEach(x => x * x); //
8598+
8599+
The `forEach()` method in JavaScript always returns undefined. This is because forEach() is used to iterate over arrays and perform side effects on each element, rather than returning a `new array or transforming the original array`
85988600
```
85998601
86008602
2. **Chaining methods:** The `map` method is chainable. i.e, It can be attached with `reduce`, `filter`, `sort` and other methods as well. Whereas `forEach` cannot be attached with any other methods because it returns `undefined` value.

0 commit comments

Comments
 (0)