nnoor - 1 month ago 12

Javascript Question

and objects are both iterable objects. Both store object by pair. I want to know when to use what? Is there any preference one over another?

`Set`

`[key, value]`

Answer

Provided you are talking about the ES6 types, they aren't the same data structure even though the `Set`

might be implemented with a `Map`

.

Your definition of `Map`

is right, but a `Set`

is a collection of unique values, unlike an array which can have duplicates.

```
var array = [1, 2, 3, 3];
var set = new Set(array); // Will have [1, 2, 3]
assert(set.size, 3);
var map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
map.set('C', 3);
map.set('a', 4); // Has: a, 4; b, 2; c: 3, C: 3
assert(map.size, 4);
```