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

Is there any advantage to using Array.from with a querySelectorAll()

In a tutorial I saw the following:

Array.from( template.querySelectorAll('.hw-text') )
  .forEach( n => n.textContent = hwMsg );

But the following line would also work.

template.querySelectorAll('.hw-text').forEach( n => n.textContent = hwMsg );

Now I wonder why in the tuturial Array.from is used and what advantage you get from it.

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 :

forEach was not part of the original NodeList specification. Browser support for querySelectorAll and Array.from is wider than for forEach on NodeLists.

Whether that additional browser support is worth it today is partly a matter of opinion and partly dependant on the website’s target audience.

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