Need to keep a player’s skills from appearing twice? A JavaScript Set holds unique values: adding a value already present does not add another entry. It is iterable, so you can traverse it with for..of or spread it into an array. section 24.0
Create a set
Use `new Set()` for an empty set, or pass an iterable: `new Set(['red', 'black', 'red'])` contains only `red` and `black`. The constructor takes the iterable’s individual values. Thus `new Set('Hello')` contains the characters (with only one `l`), while `new Set(['Hello'])` contains the whole string as one value. To copy a set, pass it to `new Set(existingSet)`. section 24.1
The constructor has no Set-literal alternative. A non-iterable argument such as `36` causes an error; use `new Set([36])` to start with one number. section 24.1
Choose operations that match what you need to do:
- Use `has(value)` to check membership, `add(value)` to remember a value, and `delete(value)` to remove it. Read `size` for the number of entries. section 24.2
- Prefer an array when you need indices or `splice`. For occasional array operations on a set, spread it into an array—but changing that array does not itself change the set. Delete a removed value from the set, or clear and refill it when rearranging entries. section 24.2
Listing 24.1. Drawing the next frame
In a tile-rendering loop, remove tiles absent from the next frame; then paint and add only tiles not already recorded. The two passes keep the set aligned with what is on screen. section 24.2
Sets also work for a card deck: iterate sets of suits and faces to add every combination. Entries keep insertion order, but have no indices. To shuffle, spread the deck into an array, swap array entries, then clear and refill the set. Try adapting that pattern when you need to reorder another collection of unique values. section 26.1














