Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

551
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda