Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Understanding map with comma operator

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading