8

I was just going through the MDN documentation for set , and how it works , coming to the part of how to iterate over a set , i saw the following examples:

// logs the items in the order: 1, "some text", {"a": 1, "b": 2} 
for (let item of mySet) console.log(item);

And

// logs the items in the order: 1, "some text", {"a": 1, "b": 2} 
for (let item of mySet.values()) console.log(item);

And

// logs the items in the order: 1, "some text", {"a": 1, "b": 2} 
//(key and value are the same here)
for (let [key, value] of mySet.entries()) console.log(key);

Just to confirm , does this mean that when using set the keys and values are the same ?

1
  • 1
    If a Set would contain key/value pairs how would it differ from Map then? Commented Nov 3, 2017 at 16:43

1 Answer 1

8

The entries() method returns a new Iterator object that contains an array of [value, value] for each element in the Set object, in insertion order [...].

MDN docs

So no, Sets don't have keys at all, however .entries() lets you believe so for having consistency between Maps and Sets.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect ! that explain it :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.