Skip to content

Commit a6b3707

Browse files
authored
Merge pull request #295 from sudo-abhinav/abhinav_branch
added why forEach loop not returning undefined
2 parents 5d06a92 + bbd51bf commit a6b3707

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8625,7 +8625,9 @@ The execution context is created when a function is called. The function's code
86258625
```javascript
86268626
const arr = [1, 2, 3, 4, 5];
86278627
arr.map(x => x * x); // [1, 4, 9, 16, 25]
8628-
arr.forEach(x => x * x); // undefined
8628+
arr.forEach(x => x * x); //
8629+
8630+
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`
86298631
```
86308632
86318633
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)