Skip to content

Commit

Permalink
Merge pull request #295 from sudo-abhinav/abhinav_branch
Browse files Browse the repository at this point in the history
added why forEach loop not returning undefined
  • Loading branch information
sudheerj authored Jul 18, 2024
2 parents 5d06a92 + bbd51bf commit a6b3707
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8625,7 +8625,9 @@ The execution context is created when a function is called. The function's code
```javascript
const arr = [1, 2, 3, 4, 5];
arr.map(x => x * x); // [1, 4, 9, 16, 25]
arr.forEach(x => x * x); // undefined
arr.forEach(x => x * x); //

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`
```
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.
Expand Down

0 comments on commit a6b3707

Please sign in to comment.