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

557
Vistas
What's the difference between keys(), values(), entries() and Symbol.iterator?

There are iterable and non-iterable objects in JS. If the object contains Symbol.iterator() function then it's iterable. If the object doesn't contain this function, then it's non-iterable. Why? This function returns iterator object.

The source

The well-known Symbol.iterator symbol specifies the default iterator for an object. Used by for...of.

It's okay. But there are other functions: keys(), values(), entries(). These functions return iterator too. What's the difference between these methods and function Symbol.iterator()?

It turns out that both of these method are in prototype.

enter image description here

So, what the difference between keys(), values(), entries() and Symbol.iterator()? Looks like they both return iterator objects.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Properties like entries, keys, and values are there for convenience. They allow easy iteration over the content of the Map instance. Note also from the image you posted that entries is also the value of the Symbol.iterator property.

The Symbol.iterator property is present so that code like

for (entry of someMap)

will behave the same as

for (entry of someMap.entries())

The .keys() and .values() methods allow iterating over one part of the Map instance or the other, in situations where you only care about the keys, or only about the values. Clearly .entries() would allow that as well, via deconstructing assignment or simple array indexing.

Ultimately, there are often no satisfactory fundamental answers to questions like "why does language x do this?" or "why does API x do this?" because they're things designed by people who had their own reasons.

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