DEV Community

Discussion on: Can .map() Mutate The Original Array? Yes.

Collapse
 
lofiandcode profile image
Joseph Trettevik • Edited

Thank you so much for the feedback. I’m always trying to learn more, so I appreciate you taking the time to read and comment on my blog post.

You’re absolutely right that Array.prototype.map() will not mutate the original array by itself, which is why that’s the first thing I mentioned at the beginning of this post.

Now was my title for this post a little on the clickbaity side of things? Sure. But my point was that if we’re not thoughtful in the way we write the callback function, Array.prototype.map() can mutate the original array.

This post is intended for JavaScript beginners, and if as a beginner you believe that .map() will never mutate the original array, you’re going to run into this issue and have no idea why your original array is getting mutated.

I think we agree on the subject, we just disagree on the semantics.

You’re essentially saying that Array.prototype.map() doesn’t mutate the original array, developers who use Array.prototype.map() mutate the original array.

And you’re right, but at the end of the day Array.prototype.map() was executed and the original array got mutated.

Array.prototype.map() never executes itself. There is always a developer behind it.

Finally, thanks for pointing out how you can rewrite the arrow function in my last example to get a different behavior. That’s pretty cool and I hadn’t thought of that!