--- id: javascript-sets title: "JavaScript Sets" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Set", "new Set", "unique values", "ES6 Set", "Set add", "Set collection"] 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", "set", "collection", "es6"] raw_sources: ["https://www.w3schools.com/js/js_sets.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Sets]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A JavaScript Set is a collection of unique values β€” each value can occur only once β€” built either from an array or by adding values one at a time, and iterable with `for...of`. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **A Set is a collection of unique values.** Each value can only occur once in a Set, and values can be of any type β€” primitive values or objects. [S1] - **Two ways to create a Set** β€” pass an array to `new Set()`, or create an empty `new Set()` and use `add()`. [S1] - **`add()` ignores duplicates** β€” adding a value that already exists has no effect; only the first occurrence is retained. [S1] - **Sets are iterable** β€” a `for...of` loop lists all elements. [S1] - **Type identity** β€” `typeof` a Set returns `"object"`, and `instanceof Set` returns `true`. [S1] - **Set is an ES6 feature** with full support in all modern browsers since June 2017. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Deduplicate by construction** β€” feeding values into a Set automatically discards repeats. [S1] - **Empty-then-populate** β€” `const s = new Set();` followed by repeated `s.add(...)`. [S1] - **Add variables, not just literals** β€” `add()` accepts any value, including variables holding objects. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) A JavaScript Set is a collection of unique values. Each value can only occur once in a Set. The values can be of any type, primitive values or objects. [S1] **How to create a Set** β€” you can create a JavaScript Set by passing an array to `new Set()`, or create an empty Set and add values. [S1] **The new Set() Method** β€” pass an array to the `new Set()` constructor: [S1] ```javascript // Create a Set const letters = new Set(["a","b","c"]); ``` Create an empty Set and add values with `add()`: [S1] ```javascript // Create a Set const letters = new Set(); // Add Values to the Set letters.add("a"); letters.add("b"); letters.add("c"); ``` You can add variables to a Set: [S1] ```javascript // Create a Set const letters = new Set(); // Create Variables const a = "a"; const b = "b"; const c = "c"; // Add Variables to the Set letters.add(a); letters.add(b); letters.add(c); ``` **The add() Method** [S1] ```javascript letters.add("d"); letters.add("e"); ``` If you add equal elements, only the first will be saved: [S1] ```javascript letters.add("a"); letters.add("b"); letters.add("c"); letters.add("c"); letters.add("c"); letters.add("c"); letters.add("c"); letters.add("c"); ``` **Listing the Elements** β€” you can list all the elements of a Set with a `for...of` loop: [S1] ```javascript // Create a Set const letters = new Set(["a","b","c"]); // List all Elements let text = ""; for (const x of letters) { text += x; } ``` **A JavaScript Set is an Object** [S1] ```javascript typeof letters; // Returns object ``` ```javascript letters instanceof Set; // Returns true ``` **Browser Support** β€” Set 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 `letters` Set from an array or by adding values, demonstrating that duplicate `add()` calls collapse to one, and listing elements with `for...of`. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Create a Set from an array (language: JavaScript): ```javascript const letters = new Set(["a","b","c"]); ``` Iterate a Set: ```javascript let text = ""; for (const x of letters) { text += x; } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (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 Set Methods]], [[JavaScript Set Logic]], [[JavaScript Maps]] - **μ°Έμ‘° λ§₯락:** The base concept referenced whenever a collection of unique values is needed. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Sets β€” https://www.w3schools.com/js/js_sets.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Sets" page (Astra wiki-curation, P-Reinforce v3.1 format).