Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda