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

550
Views
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 answers
Answer question

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 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!