I just want to address the comments saying map is an alternative to forEach (@brieucp
), or that array.map(console.log) is a good thing to do (@latsuj
).
Please make sure you understand the differences between forEach and map before using them, specifically around what each method returns. The differences between the two methods could be their own article, but they are not interchangeable.
array.map(console.log) is bad practice and, in this instance, forEach should be used.
Read up on the differences. If you don't understand after that, reach out and we can discuss it.
I detailed (and rephrased) my comment to avoid misinterpretation.
about array.map(console.log) vs array.forEach(console.log), we should avoid both of them and alaways expand the declaration of the function. Because what's being log is probably not what you want .
I'd argue that array.forEach(console.log) is acceptable in some circumstances. For example, if you're developing a new function and want to see want to log each element for debug purposes.
But generally, in a production setting, logging every element in an array is not good practice regardless of the method you choose.
I wasn't talking about the console.log, I was about the fact that a funny thing happen when you do something like [1,2,3].forEach(console.log) (same with map)
1 0 [ 1, 2, 3 ]
2 1 [ 1, 2, 3 ]
3 2 [ 1, 2, 3 ]
Because forEach and map are passing three params on each call (the element, its index and the full list) which is probably not what you want. It's true with all function that takes more than one input.
I just want to address the comments saying
map
is an alternative toforEach
(@brieucp ), or thatarray.map(console.log)
is a good thing to do (@latsuj ).Please make sure you understand the differences between
forEach
andmap
before using them, specifically around what each method returns. The differences between the two methods could be their own article, but they are not interchangeable.array.map(console.log)
is bad practice and, in this instance,forEach
should be used.Read up on the differences. If you don't understand after that, reach out and we can discuss it.
I detailed (and rephrased) my comment to avoid misinterpretation.
about array.map(console.log) vs array.forEach(console.log), we should avoid both of them and alaways expand the declaration of the function. Because what's being log is probably not what you want .
Appreciate the follow up @brieucp !
I'd argue that
array.forEach(console.log)
is acceptable in some circumstances. For example, if you're developing a new function and want to see want to log each element for debug purposes.But generally, in a production setting, logging every element in an array is not good practice regardless of the method you choose.
I wasn't talking about the console.log, I was about the fact that a funny thing happen when you do something like [1,2,3].forEach(console.log) (same with map)
Because forEach and map are passing three params on each call (the element, its index and the full list) which is probably not what you want. It's true with all function that takes more than one input.
Ah I see what point you were making now!
Then yeah, you're obviously correct 😊.
Sorry, I got the wrong end of the stick!
Explain why using array.map(nameOfFunction) is a bad practice.
I'm ready to hear anything if it's logic.
I've not said using
array.map(nameOfFunction)
is bad practice, I said usingconsole.log
in that way is.Your comment used
array.map(console.log)
which is just wrong.Haaa ok,
I just took the same examples as the article. But i do agree that console.log is bad practice.