I am trying to understand some confusing code I came across (original)
Here is a simplified version:
[1,2,3].map(v => alert(v), alert('first'))
Why is ‘first’ shows first ? and why is it evaluated only once ?
>Solution :
The return value of alert('first') is being passed as the second argument to Array#map, so it is evaluated before map is called.
In this case, the second parameter of map is the value to set as this of the callback. Note that you can also pass arguments that a function is not even declared to take as parameters.