Files
2nd/10_Wiki/Topic_JavaScript/JavaScript_Set_Methods.md
T
koriweb 9609c04755 docs(10_Wiki): W3Schools 위키화 — HTML/CSS/JavaScript(core)
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더).
- Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외)
- Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체)
- Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속)
각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 19:21:18 +09:00

232 lines
7.1 KiB
Markdown

---
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).