Chapter 12 · Simulation

Map Simulator

← Chapter 12

Insertion order

Contacts

Result

—

History

  1. No operations yet.

Map Simulator

A map stores values under unique keys. set(key, value) adds a new entry or updates the value of an existing key. A JavaScript Map iterates over its entries in insertion order.

Controls

  • Type a name (the key) and an email address (the value), then choose Add / Update (set), Get (get), Has? (has) or Delete (delete). The animation compares the key with each stored entry in insertion order.
  • Iterate Entries, Show Keys and Show Values walk the map in insertion order and print the output in the result box.
  • The simulator holds at most 6 entries. Cancel stops the running operation without changing the map.

Map concepts

  • Unique keys: a map holds at most one entry per key. Setting an existing key again leaves the size alone and replaces only the value.
  • Insertion order: updating an existing key does not move it to the end, but deleting a key and adding it again gives it a new position.
  • Map vs. Object: a Map accepts keys of any type, has a size property of its own, and is built for frequent insertions, lookups and deletions.
  • Safe display: names and email addresses you type are shown as text nodes and are never interpreted as HTML or JavaScript.

The core JavaScript Map API

Method / propertyPurpose
map.set(key, value)Adds or updates an entry; returns the Map.
map.get(key)Returns the value, or undefined.
map.has(key)Tests whether the key is present.
map.delete(key)Removes the entry and returns a Boolean.
map.sizeReturns the number of entries.
map.clear()Removes every entry.