Hacker News new | ask | show | jobs
by alephnan 2323 days ago
> Once, a student asked about the difference between JavaScript array methods, like push versus concat or forEach versus map, the team lead said they were interchangeable. (They are not.)

Technically, push and concat can be augmented to achieve the same thing. Same with forEach and map. Might not be the best fitted screwdriver but it’ll still kinda work.

1 comments

Except that push mutates the array in question and concat returns a deep copy. And forEach returns undefined, while map returns an array. If someone claimed they were the same in an interview I doubt they would get hired.
Technically, yes there is a difference in semantics and convenience. pragmatically it depends on the programming style and language features / expressiveness.

Developers were able to map from one list to another list using for loops before functional-style map-like functions. Map is just semantically more concise about what it’s doing, rather than how.

As for mutation versus immutable copy behavior, that’s for convenience. Developers can also clone collections themselves.

A more charitable means of evaluating the class lead’s comment is that the core of an algorithm is not affected by these things.

One could argue multiplication is just repeated addition.

Do you mean a shallow copy?