liveBook · Manning

Set

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A JavaScript collection type that stores unique values and provides iterable traversal over its elements.

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
function draw(nextFrame) { for (const tile of frame) { 1 if ( !nextFrame.has(tile) ) { 2 frame.delete(tile); 3 } } for (const tile of nextFrame) { 4 if ( !frame.has(tile) ) { 5 // paint the tile to the screen frame.add(tile); 6 } } }

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

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Works through construction, membership updates, array conversion, and shuffling; stops short of implementing Set internals.
Examples
Uses duplicate colors, a tile-rendering loop, and a shuffled card deck.
What it assumes
Assumes familiarity with arrays and basic JavaScript loops while teaching Set construction from an empty set onward.
What you can do after
Build it from scratch to maintain unique values, update membership, and convert through an array when ordering operations require one.
How it connects
Connects Sets to iterables, arrays, spread, for..of, and Maps.

Also explained in

Compare how 15 books explain set →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.