I’m trying to understand the difference between map() and forEach(). When does one use one over the other? Presumably both take each element in the array and does what ever you put in the function for each element and returns an array. Can some one else explain the difference? Thanks.
There are other use cases but the above gives us the gist. When we wish to only perform an action and not return any data, such as logging like above, then forEach is the iterator to substitute in the place of a for loop. When we wish to transform the data into a new array, then map is the iterator to reach for.