According to mdn's "Set" documentation, entries is provided as a convenience method

...to keep the API similar to the Map object, each entry has the same value for its key and value here, so that an array [value, value] is returned.

My question is what is the purpose of keeping the API similar in this case? i.e. when would I ever use the method entries for a Set?

  • Not enough for a full answer, but one useful corner case is when "attaching" an object instance that is based on previously-retrieved data from a different context (such as having "Get" and "Save" methods in a service layer). In this case, you need to specifically mark the instance you attach as being "Modified" so EF will generate the correct update statement, and you do that through the Entries dictionary.
    – KeithS
    Apr 17, 2017 at 15:01

1 Answer 1


Set and Map have slightly different APIs in terms of their stored values. While Sets contain values only, Maps contain their associated keys as well.

Because of this, should both of the structures have filter, map, forEach,... functions just like Array does, the action setting presumtion about each iterated value is likely to be slightly different, specifically:

Map: function(key, value) { }
Set: function(value) { }

Because when you already keep keys in a Map it makes sense to be able to inspect the value of the key during iteration as well.

Now if you encountered a situation where you would like to iterate over any of the two collections but cared only about the value, you would need two functions, one for each data collection.

map.forEach((key, value) => console.log(value));
set.forEach(value => console.log(value));

Even though you only care about the value and the logic of both actions is clearly the same, you cannot write a function to serve both collections. The entries method of both Set and Map fixes this issue by providing a common interface.

By creating the following function (or its alternative, I am not manily a JS developer so please excuse any inaccuracies):

function forEach(entriesIterator, action) {
    let current = entriesIterator.next();
    while (current.done === false) {

        current = entriesIterator.next();

you can feed either entries of Map and/or Set and it will still work.

forEach(map.entries(), value => console.log(value));
forEach(set.entries(), value => console.log(value));

TLDR: The entries method provides an abstraction to iterate over a data collection where you may potentially only care about the values.

  • 1
    You have the right idea conceptually but nothing prevents the function from taking the value first and the key second, which is precisely what a callback passed to Map.prototype.forEach must do because it receives (value, key) for each pair. Also, Map and Set also expose values methods that just return the values so it doesn't really answer the question. Dec 18, 2016 at 13:25
  • 2
    @AluanHaddad OP was specifically asking why would you ever use the entries method, not asking which other methods Set and Map have.
    – Andy
    Dec 18, 2016 at 17:38
  • 1
    That is true. However, it is still possible to write a function that works for both sets and maps without the entries method. Dec 18, 2016 at 17:57
  • 1
    @AluanHaddad Even from the little experience with JS I have I really do not like altering prototypes of system types. Seems like monkey patching and you would need to alter prototypes of both Set and Map anyway. Personally I think the entries approach is leaner.
    – Andy
    Dec 18, 2016 at 19:25
  • 1
    Why not just use the values method. I am not suggesting any prototype modifications. Dec 18, 2016 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.