The params of your forEach (item, index) will get passed to the console.log function :)
if you are using custom functions to pass into things like .forEach you can go from this example:
functionprintMessage(msg){console.log(msg);}
to this
constprintMessage=(msg)=>console.log(msg);
Hope that helps someone!
Edit: adding example of printMessage
['🍉','🍊','🍇','🍎'].forEach(printMessage);
Final note: doing this kind of extraction/abstraction of functionality can help better describe what's happening in your code without needing to add code comments and it can help you unit test your code later on with more context. When you build a larger system, you could have these type of helper functions extracted out of your main code as well. 🚀
A passionate programmer & front-end developer. I like to share HTML, CSS, JavaScript and web development tips & tricks and try to explain them in a simple manner.
If you don't need the
this
context you can shorten even further when you use arrow functions.in your example
it can be shortened to
The params of your forEach (item, index) will get passed to the console.log function :)
if you are using custom functions to pass into things like
.forEach
you can go from this example:to this
Hope that helps someone!
Edit: adding example of printMessage
Final note: doing this kind of extraction/abstraction of functionality can help better describe what's happening in your code without needing to add code comments and it can help you unit test your code later on with more context. When you build a larger system, you could have these type of helper functions extracted out of your main code as well. 🚀
Thanks for sharing! 😊