Skip to content

Maps: look up by key

Read · 1 of 4

A Map links keys to values

const count = new Map(); starts empty. count.set(key, value) stores, count.get(key) reads (undefined if missing) and count.has(key) checks.

count.set(w, (count.get(w) || 0) + 1) counts in one line: || 0 turns a missing count into 0.

A Map keeps keys in the order they were added. For alphabetical order, sort them: [...count.keys()].sort().