typestar

Map and Set in JavaScript

Keyed collections with any key type, and unique sets.

const cache = new Map();
cache.set("ada", 95);
cache.set("grace", 88);

const score = cache.get("ada");
const known = cache.has("kay");
cache.delete("grace");
const size = cache.size;

const tags = new Set(["a", "b", "a", "c"]);
tags.add("d");
const unique = [...tags];
const hasB = tags.has("b");

How it works

  1. Map has set, get, has, delete, and size.
  2. Set stores unique values; duplicates collapse.
  3. Spreading a Set yields a deduplicated array.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
297
Tokens
102
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 64 seconds.

Type this snippet

Step 3 of 8 in Objects & collections, step 29 of 43 in Language basics.

← Previous Next →