Map + Callback Function with Optional Args = 🙀

Yet another JavaScript pitfall.

Map + Callback Function with Optional Args = 🙀

I have encountered this uncanny behavior of JavaScript. Long story short, when you pass to a map() a callback function with optional parameters, those parameters are bound to be filled with the current element's index (and the original array if the callback function can take 3 arguments in total). So, be explicit about what you intend to pass to the callback function.

Reference

Array.prototype.map() - JavaScript | MDN
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.