--- id: javascript-symbols title: "JavaScript Symbols" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["JS symbols", "Symbol()", "Symbol.for", "Symbol.iterator", "unique identifier"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "symbol", "data-types", "unique-identifier"] raw_sources: ["https://www.w3schools.com/js/js_datatypes_symbol.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Symbols]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A Symbol is a primitive that represents a unique identifier β€” every Symbol value is distinct even with identical descriptions β€” making it ideal for hidden, collision-free object property keys. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Symbol = unique identifier** β€” a Symbol represents a unique identifier; every Symbol value is distinct, even when created with the same description. [S1] - **Hidden identifiers** β€” Symbols act as hidden identifiers that no code can accidentally access, preventing property name conflicts when multiple developers work with shared objects. [S1] - **Optional description** β€” `Symbol("id")` attaches a description (label) without affecting uniqueness. [S1] - **`Symbol.for()` is global/shared** β€” symbols created with `Symbol.for(key)` are looked up in a global registry, so two `Symbol.for("id")` calls return the same symbol. [S1] - **`typeof` a symbol is `"symbol"`** β€” Symbols have their own primitive type. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Symbol as object key** β€” use `person[id] = 123;` (with `id` a Symbol) to add a property that won't collide with string keys. [S1] - **Hidden from enumeration/serialization** β€” Symbol properties are excluded from `for...in` loops and ignored by `JSON.stringify()`. [S1] - **`Symbol.iterator` makes objects iterable** β€” define a `[Symbol.iterator]()` method returning a `next()`-based iterator to support `for...of`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **JavaScript Symbols** A Symbol represents a unique identifier. Symbols are hidden identifiers that no code can accidentally access, which helps prevent accidental property conflicts. Every Symbol value is distinct, even with identical descriptions. [S1] **Creating Symbols** [S1] ```javascript const id1 = Symbol(); const id2 = Symbol(); ``` **Symbol Descriptions** [S1] ```javascript const id = Symbol("id"); ``` **Uniqueness** β€” two symbols with the same description are still different: [S1] ```javascript const id1 = Symbol("id"); const id2 = Symbol("id"); let result = (id1 === id2); ``` **Symbol as Object Property Key** [S1] ```javascript const id = Symbol("id"); const person = { firstName: "John", lastName: "Doe" }; person[id] = 123; ``` **Type Checking** β€” `typeof` of a symbol is `"symbol"`: [S1] ```javascript const id = Symbol("id"); let type = typeof id; ``` **Global Symbols** β€” `Symbol.for()` reuses a shared symbol from a global registry: [S1] ```javascript const id1 = Symbol.for("id"); const id2 = Symbol.for("id"); let result = (id1 === id2); ``` **`Symbol.iterator` Implementation** β€” define a custom iterator so the object works with `for...of`: [S1] ```javascript const myObject = { data: ["A", "B", "C"], [Symbol.iterator]() { let index = 0; let data = this.data; return { next() { if (index < data.length) { return {value:data[index++], done:false}; } else { return {done:true}; } } }; } }; let text = ""; for (const x of myObject) { text += x + "
"; } ``` **Notes on behavior** - **`for...in` loops** β€” Symbol properties are excluded from `for...in` iteration, maintaining their hidden nature. [S1] - **JSON serialization** β€” `JSON.stringify()` ignores Symbol properties entirely. [S1] - **When to use** β€” recommended for unique property names, hidden properties, custom iterables, and special behaviors, but cautioned against routine use in everyday code. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” creating symbols, using one as a `person[id]` key, the `Symbol.for()` global lookup, and the `Symbol.iterator` custom iterable. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Use a Symbol as a collision-free property key (language: JavaScript): ```javascript const id = Symbol("id"); person[id] = 123; ``` Shared global symbol: ```javascript const id1 = Symbol.for("id"); const id2 = Symbol.for("id"); let result = (id1 === id2); ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`Symbol()` vs `Symbol.for()`** β€” `Symbol()` always creates a brand-new, unique symbol (even with the same description); `Symbol.for(key)` returns a shared symbol from the global registry, so repeated calls with the same key are equal. Use `Symbol()` for truly private keys and `Symbol.for()` when the same symbol must be retrievable across code. [S1] - **Symbol key vs string key** β€” Symbol keys are hidden from `for...in` and `JSON.stringify()`, unlike string keys; choose symbols when a property should not be enumerated or serialized, but prefer ordinary keys for everyday data. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. The page advises against routine use of Symbols in everyday code. [S1] ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.87 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Data Types]], [[JavaScript Iterables]], [[JavaScript Iterators]], [[JavaScript typeof]] - **μ°Έμ‘° λ§₯락:** Referenced when defining hidden/unique object keys or implementing `Symbol.iterator` for custom iterables. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Symbols β€” https://www.w3schools.com/js/js_datatypes_symbol.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Symbols" page (Astra wiki-curation, P-Reinforce v3.1 format).