Table of Contents
forEach
Trying to return a value
Using the array name for the element
filter
Using it to transform elements
forEach
Trying to return a value
Can you figure out the issue in the below code:
Issue:
forEach doesn't return a new array. It always returns undefined.
Fixed Code:
Instead of returning directly from the loop, create a new array and push the values to the new array.
Using the array name for the element
Can you figure out the issue in the below code:
Issue:
Using the array name (numbers) as the parameter name in the callback function shadows the original array and can lead to unexpected behaviour.
Fixed Code:
Use a different variable name for the variable used for the element
filter
Using it to transform elements
Can you figure out the issue in the below code:
Issue:
filter is used to select elements, not transform them. It keeps elements for which the callback returns true.
Fixed Code:
To transform elements we should use a map.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article