Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

Element.querySelectorAll() returns a NodeList this object has a method called forEach which lets you to iterate over its items.

that same NodeList Object has length property, so you can pass it to Array.from which will return back an array, so you have access to all array methods and that's why they used it in the tutorial.

about 4 years ago · Juan Pablo Isaza Report

0

Not sure about the tutorial intention, but you can not call array functions on the NodeList object returned by querySelectorAll().

 document.querySelectorAll('.hw-text').map( ... ) // throws error
 [...document.querySelectorAll('.hw-text')].map( ... ) // works just fine
 Array.from(document.querySelectorAll('.hw-text')).map( ... ) // works just fine
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!