--- id: javascript-set-methods title: "JavaScript Set Methods" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Set methods", "Set.add", "Set.has", "Set.size", "Set.values", "Set.entries", "Set.forEach"] 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", "methods"] raw_sources: ["https://www.w3schools.com/js/js_set_methods.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Set Methods]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The core Set methods β€” `add`, `delete`, `clear`, `has`, `forEach`, `values`, `keys`, `entries`, plus the `size` property β€” manage and iterate a unique-value collection; because a Set has no keys, `keys()` and `entries()` mirror `values()` to stay compatible with Maps. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Sets only store unique values** β€” if you add an element that already exists, `add()` has no effect and the Set is unchanged. [S1] - **`size` is a property, not a method** β€” it returns the number of elements. [S1] - **`has()` tests membership** β€” returns `true` if a value exists in the Set. [S1] - **`forEach()` runs a callback per element.** [S1] - **`values()` returns an Iterator** of the Set's values. [S1] - **A Set has no keys** β€” so `keys()` returns the same as `values()`, and `entries()` returns `[value,value]` pairs. This makes Sets compatible with Maps. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Iterator-then-loop** β€” capture `letters.values()` (or `keys()`/`entries()`) into a variable, then `for...of` it; or loop the call directly. [S1] - **Membership guard** β€” `letters.has(x)` before acting on a value. [S1] - **forEach for side effects** β€” accumulate or process each value via a callback. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **The new Set() Method** β€” pass an array to the `new Set()` constructor: [S1] ```javascript // Create a new Set const letters = new Set(["a","b","c"]); ``` **The add() Method** [S1] ```javascript letters.add("d"); letters.add("e"); ``` Sets only store unique values. If an attempt is made to add an element that already exists in the Set, the `add()` method will have no effect, and the Set will remain unchanged: [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"); ``` **The size Property** β€” returns the number of elements: [S1] ```javascript // Create a new Set const mySet = new Set(["a","b","c"]); // The number of elements are mySet.size; ``` **Listing Set Elements** β€” list all elements 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; } ``` **The has() Method** β€” returns `true` if a value exists: [S1] ```javascript // Create a Set const letters = new Set(["a","b","c"]); // Does the Set contain "d"? answer = letters.has("d"); ``` **The forEach() Method** β€” invokes a callback for each element: [S1] ```javascript // Create a Set const letters = new Set(["a","b","c"]); // List all entries let text = ""; letters.forEach (function(value) { text += value; }) ``` **The values() Method** β€” returns an Iterator with the values in a Set: [S1] ```javascript // Create a Set const letters = new Set(["a","b","c"]); // Get all Values const myIterator = letters.values(); // List all Values let text = ""; for (const entry of myIterator) { text += entry; } ``` ```javascript // Create a Set const letters = new Set(["a","b","c"]); // List all Values let text = ""; for (const entry of letters.values()) { text += entry; } ``` **The keys() Method** β€” a Set has no keys, so `keys()` returns the same as `values()`. This makes Sets compatible with Maps: [S1] ```javascript // Create a Set const letters = new Set(["a","b","c"]); // Create an Iterator const myIterator = letters.keys(); // List all Elements let text = ""; for (const x of myIterator) { text += x; } ``` ```javascript // Create a Set const letters = new Set(["a","b","c"]); // List all Elements let text = ""; for (const x of letters.keys()) { text += x; } ``` **The entries() Method** β€” a Set has no keys, so the `entries()` method returns `[value,value]`. This makes Sets compatible with Maps: [S1] ```javascript // Create a Set const letters = new Set(["a","b","c"]); // Get all Entries const myIterator = letters.entries(); // List all Entries let text = ""; for (const entry of myIterator) { text += entry; } ``` ```javascript // Create a Set const letters = new Set(["a","b","c"]); // List all Entries let text = ""; for (const entry of letters.entries()) { text += entry; } ``` **Set Methods and Properties** [S1] | Method/Property | Description | |---|---| | new Set() | Creates a new Set | | add() | Adds a new element to the Set | | clear() | Removes all elements from a Set | | delete() | Removes a specified element from a Set | | entries() | Returns an Iterator with [value,value] pairs from a Set | | forEach() | Invokes a callback for each element in the Set | | has() | Returns true if a value exists in the Set | | keys() | Returns an Iterator with the values in a Set | | values() | Returns an Iterator with the values in a Set | | size | Returns the number of elements in a Set | ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” building a `letters`/`mySet` Set, reading `size`, testing membership with `has()`, and walking the Set with `forEach()`, `values()`, `keys()`, and `entries()`. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Membership test (language: JavaScript): ```javascript answer = letters.has("d"); ``` Iterate values via the values() iterator: ```javascript let text = ""; for (const entry of letters.values()) { text += entry; } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. Note the intentional design choice: `keys()` and `entries()` echo `values()` because Sets have no keys β€” done deliberately for Map compatibility, not a bug. [S1] ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Sets]], [[JavaScript Set Logic]], [[JavaScript Map Methods]] - **μ°Έμ‘° λ§₯락:** Referenced whenever managing or iterating the contents of a Set. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Set Methods β€” https://www.w3schools.com/js/js_set_methods.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Set Methods" page (Astra wiki-curation, P-Reinforce v3.1 format).