What a set is
An array keeps every value you put in it, repeats included. Sometimes you only need to know which values there are. A Set is made for that: it holds each value once, and adding a value that is already in it changes nothing.
let pets = new Set(["cat", "dog", "cat"]); // cat goes in once
pets.add("fish"); // a new value
pets.add("dog"); // already in it, no change
console.log(pets.size); // 3
console.log(pets.has("dog")); // true
The four things to know
new Set()makes an empty set.s.add(x)putsxin the sets.s.has(x)istruewhenxis in the set.s.sizesays how many values the set holds.
new Set(arr) builds a set from an array, and the repeats go in once. A string works too, because it is iterable: new Set("hello").size is 4, since the two l letters count once.
Checking s.has(x) is fast, even for a big set. An array's includes looks at the values one by one, so on a long array it takes longer.
From an array to a set and back
A common use of a set is to remove repeats from an array. Build a set from the array, then spread the set into a new array with [...]. The values keep the order in which they were first added:
let names = ["Ava", "Leo", "Ava", "Mia", "Leo"];
let unique = [...new Set(names)];
console.log(unique);
console.log(unique.length);
[ 'Ava', 'Leo', 'Mia' ]
3
Notice that the new array has a length, because it is an array again. You can also start from an empty set and fill it one value at a time with add. Calls to add can be chained, since each one returns the set: after seen.add("Ava").add("Leo"), the size is 2.
Traps
- The names differ from arrays. A set counts with
size, notlength, and checks withhas, notincludes. For the setpetsabove,pets.lengthandpets.includesare bothundefined. - No indexes. A JavaScript set remembers the order you added values in, but it has no positions:
pets[0]isundefined. To get an array back, spread it:[...pets]. - Values are compared exactly. The number
1and the string"1"are different values, sonew Set([1, "1"]).sizeis 2. - Two sets are never
===just because they hold the same values. The comparison is about the object itself, not its contents. addreturns the set. You can ignore the result, which is what the exercise does.
The exercise, step by step
The camp's sign-up array names has some names twice. The task is to build a set kids from it, add "Zoe" and then "Ava", and print Leo is coming if kids has "Leo". The starter code already prints the number of kids twice.
- Build the set below
names:let kids = new Set(names);. Ava and Leo are in the array twice, but each goes in the set once, sokidsholds 3 names. - Below the first
console.log, calladdwith "Zoe" and then with "Ava". Only Zoe makes the set bigger; Ava is already in it. - At the end, check
kids.has("Leo")and print the message.
let names = ["Ava", "Leo", "Ava", "Mia", "Leo"];
let kids = new Set(names);
console.log(kids.size + " kids");
kids.add("Zoe");
kids.add("Ava");
console.log(kids.size + " kids");
if (kids.has("Leo")) {
console.log("Leo is coming");
}
The output is:
3 kids
4 kids
Leo is coming
Key takeaways
- A set holds each value once;
addon a value it already has does nothing. - Use
size,addandhas, notlength,pushandincludes. new Set(array)removes repeats in one step.- A set has no indexes, so
s[0]isundefined.
