Map keys and lookups
- Version
- es2025
- Since
- es2015
- Review
- Draft
Summary#
A Map stores key-value pairs and can use objects as keys. get retrieves a value and has distinguishes a missing key from one with an undefined value.
Syntax#
mapping.set(key, value);
const present = mapping.has(key);
const stored = mapping.get(key);
Example#
const originalKey = { id: 3 };
const lookup = new Map();
lookup.set(originalKey, "ready");
console.log(lookup.get(originalKey));
console.log(lookup.has({ id: 3 }));
Expected output#
ready
false
Common mistakes#
A fresh object does not match an existing object key just because its properties are equal. Use the same object reference, or use a primitive identifier when keys should be matched by value.
Version applicability#
Map is a standard built-in from ES2015 onward.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0