--- id: javascript-maps title: "JavaScript Maps" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Map", "new Map", "key-value pairs", "ES6 Map", "Map.set", "Map.get", "dictionary"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "map", "collection", "es6"] raw_sources: ["https://www.w3schools.com/js/js_maps.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Maps]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A JavaScript Map stores key-value pairs like a dictionary, but unlike plain objects its keys can be of any data type, it remembers insertion order, and it exposes a `size` property. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **A Map is an object that stores key-value pairs**, similar to a dictionary in other languages; keys can be of any data type. [S1] - **Maps are most similar to Objects** (a unique key/value collection), but with stronger guarantees. [S1] - **Six characteristics** β€” keys can be any type; original insertion order is remembered; item count is easy via `size`; optimized for frequent additions/removals; directly iterable with `for...of` or `forEach()`; original order preserved during iteration. [S1] - **Two ways to create a Map** β€” `new Map()` then `set()` calls, or `new Map([...])` with an array of `[key, value]` pairs. [S1] - **`set()` adds or updates** β€” calling `set()` with an existing key changes its value. [S1] - **`get()` retrieves a value** by its key. [S1] - **Type identity** β€” `typeof` a Map returns `"object"`, and `instanceof Map` returns `true`. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **set-chain construction** β€” create empty, then `set()` each pair. [S1] - **Array-literal construction** β€” pass `[[k, v], ...]` to `new Map()`. [S1] - **Upsert by key** β€” `set()` doubles as insert (new key) and update (existing key). [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) A JavaScript Map is an object that can store collections of key-value pairs, similar to a dictionary in other programming languages. Maps differ from standard objects in that keys can be of any data type. Maps are similar to both Objects (unique key/value collection) and Arrays (ordered values collection), but Maps are most similar to Objects. [S1] **Map characteristics:** keys can be any type; the original insertion order is remembered; the number of items is easily retrieved via the `size` property; optimized for frequent additions and removals; directly iterable with `for...of` loops or `forEach()`; original order preserved during iteration. [S1] **How to Create a Map** β€” you can create a Map by passing an Array to `new Map()`, or by creating a Map and using `set()`. [S1] **Using set()** β€” create an empty Map and set values: [S1] ```javascript // Create an empty Map const fruits = new Map(); // Set Map Values fruits.set("apples", 500); fruits.set("bananas", 300); fruits.set("oranges", 200); ``` **Using an Array** β€” pass an array of `[key, value]` pairs to the constructor: [S1] ```javascript // Create a Map const fruits = new Map([ ["apples", 500], ["bananas", 300], ["oranges", 200] ]); ``` **Adding Elements** β€” the `set()` method can also be used to add new elements: [S1] ```javascript fruits.set("mangos", 100); ``` **Changing Values** β€” `set()` will also change existing values: [S1] ```javascript fruits.set("apples", 200); ``` **The get() Method** β€” gets the value of a key: [S1] ```javascript fruits.get("apples"); // Returns 500 ``` **A JavaScript Map is an Object** [S1] ```javascript // Returns object: typeof fruits; ``` ```javascript // Returns true: fruits instanceof Map; ``` **JavaScript Objects vs Maps** [S1] | | Object | Map | |---|--------|-----| | Iterable | Not directly iterable | Directly iterable | | Size property | Do not have a size property | Have a size property | | Key types | Keys must be Strings (or Symbols) | Keys can be any datatype | | Key ordering | Keys are not well ordered | Keys are ordered by insertion | | Default keys | Have default keys | Do not have default keys | **Browser Support** β€” Map is an ES6 feature (JavaScript 2015), fully supported in all modern browsers since June 2017. [S1] | Browser | Version | Release | |---------|---------|---------| | Chrome | 51 | May 2016 | | Edge | 15 | Apr 2017 | | Firefox | 54 | Jun 2017 | | Safari | 10 | Sep 2016 | | Opera | 38 | Jun 2016 | ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” building a `fruits` Map by `set()` calls or array literal, adding `mangos`, updating `apples`, and reading back with `get()`. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Create a Map from an array of pairs (language: JavaScript): ```javascript const fruits = new Map([ ["apples", 500], ["bananas", 300], ["oranges", 200] ]); ``` Upsert and read: ```javascript fruits.set("apples", 200); fruits.get("apples"); ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) Per the source's Objects-vs-Maps table, prefer a Map over a plain Object when you need: direct iterability (`for...of`), a built-in `size`, keys of any data type (not just strings/symbols), reliable insertion ordering, or no inherited default keys. Use a plain Object for simple string-keyed records where those guarantees are not required. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Map Methods]], [[JavaScript Sets]], [[JavaScript Set Methods]] - **μ°Έμ‘° λ§₯락:** The base concept referenced whenever an ordered, any-key key-value collection is needed. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Maps β€” https://www.w3schools.com/js/js_maps.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Maps" page (Astra wiki-curation, P-Reinforce v3.1 format).