| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Set | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.
* Some parts of this feature may have varying levels of support.
Set . Set , Set . Set . add() Set (, add() Set ) .
Set " " . (O(1) ), (O(log(N)) ) O(N) .
. ( 0 -0 . .) , === NaN NaN (NaN !== NaN ) . () . , . Set .
has Set Set . length Set size Array.prototype.includes .
Set . .
A.difference(B) |
Set |
ABA\setminus B | |
A.intersection(B) |
Set |
ABA\cap B | |
A.symmetricDifference(B) |
Set |
(AB)(BA)(A\setminus B)\cup(B\setminus A) | |
A.union(B) |
Set |
ABA\cup B | |
A.isDisjointFrom(B) |
Boolean |
AB=A\cap B = \empty | |
A.isSubsetOf(B) |
Boolean |
ABA\subseteq B | |
A.isSupersetOf(B) |
Boolean |
ABA\supseteq B |
Set Set .
Map size, has(), keys() Set Set .
const a = new Set([1, 2, 3]);
const b = new Map([
[1, "one"],
[2, "two"],
[4, "four"],
]);
console.log(a.union(b)); // Set(4) {1, 2, 3, 4}
:
Set [Symbol.iterator]() keys() . entries has() keys Set .
has() size keys() Set . WeakSet keys() Set .
Set ( " Set ") Set API .
Set . Set Set . Set .
IDL .
GPUSupportedFeatures / Set .
IDL .
interface GPUSupportedFeatures {
readonly setlike<DOMString>;
};
Set - ( IDL readonly ).
Set size entries(), forEach(), has(), keys(), values(), Symbol.iterator() .Set clear(), delete(), add() . Set .
Set .
Set .
Set() Set .
Set.prototype Set .
Set.prototype.add()Set Set .
Set.prototype.clear()Set .
Set.prototype.delete()value . Set.prototype.has(value) false .
Set.prototype.difference()Set Set Set Set .
Set.prototype.entries()Set [value, value] . Map Set .
Set.prototype.forEach()Set callbackFn . thisArg , callbackFn this .
Set.prototype.has() Set .
Set.prototype.intersection()Set Set Set Set .
Set.prototype.isDisjointFrom()Set Set Set .
Set.prototype.isSubsetOf()Set Set Set .
Set.prototype.isSupersetOf()Set Set Set .
Set.prototype.keys()Set.prototype.symmetricDifference()Set Set Set Set Set Set .
Set.prototype.union()Set Set Set Set .
Set.prototype.values()Set yield .
[Symbol.iterator]()Set yield .
const mySet1 = new Set();
mySet1.add(1); // Set(1) { 1 }
mySet1.add(5); // Set(2) { 1, 5 }
mySet1.add(5); // Set(2) { 1, 5 }
mySet1.add("some text"); // Set(3) { 1, 5, 'some text' }
const o = { a: 1, b: 2 };
mySet1.add(o);
mySet1.add({ a: 1, b: 2 }); // o
mySet1.has(1); // true
mySet1.has(3); // false, 3 set .
mySet1.has(5); // true
mySet1.has(Math.sqrt(25)); // true
mySet1.has("Some Text".toLowerCase()); // true
mySet1.has(o); // true
mySet1.size; // 5
mySet1.delete(5); // set 5
mySet1.has(5); // false, 5 .
mySet1.size; // 4,
mySet1.add(5); // Set(5) { 1, 'some text', {...}, {...}, 5 } - , .
console.log(mySet1); // Set(5) { 1, "some text", {}, {}, 5 }
set .
for (const item of mySet1) {
console.log(item);
}
// 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5
for (const item of mySet1.keys()) {
console.log(item);
}
// 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5
for (const item of mySet1.values()) {
console.log(item);
}
// 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5
// .
for (const [key, value] of mySet1.entries()) {
console.log(key);
}
// 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5
// Array.from Set
const myArr = Array.from(mySet1); // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}, 5]
// HTML .
mySet1.add(document.body);
mySet1.has(document.querySelector("body")); // true
// Set
const mySet2 = new Set([1, 2, 3, 4]);
console.log(mySet2.size); // 4
console.log([...mySet2]); // [1, 2, 3, 4]
// (intersection) .
const intersection = new Set([...mySet1].filter((x) => mySet2.has(x)));
// (difference) .
const difference = new Set([...mySet1].filter((x) => !mySet2.has(x)));
// Set forEach()
mySet2.forEach((value) => {
console.log(value);
});
// 1
// 2
// 3
// 4
function isSuperset(set, subset) {
for (const elem of subset) {
if (!set.has(elem)) {
return false;
}
}
return true;
}
function union(setA, setB) {
const _union = new Set(setA);
for (const elem of setB) {
_union.add(elem);
}
return _union;
}
function intersection(setA, setB) {
const _intersection = new Set();
for (const elem of setB) {
if (setA.has(elem)) {
_intersection.add(elem);
}
}
return _intersection;
}
function symmetricDifference(setA, setB) {
const _difference = new Set(setA);
for (const elem of setB) {
if (_difference.has(elem)) {
_difference.delete(elem);
} else {
_difference.add(elem);
}
}
return _difference;
}
function difference(setA, setB) {
const _difference = new Set(setA);
for (const elem of setB) {
_difference.delete(elem);
}
return _difference;
}
//
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 3]);
const setC = new Set([3, 4, 5, 6]);
isSuperset(setA, setB); // true
union(setA, setC); // Set {1, 2, 3, 4, 5, 6}
intersection(setA, setC); // Set {3, 4}
symmetricDifference(setA, setC); // Set {1, 2, 5, 6}
difference(setA, setC); // Set {1, 2}
const myArray = ["value1", "value2", "value3"];
// Set Set
const mySet = new Set(myArray);
mySet.has("value1"); // returns true
// set
console.log([...mySet]); // Will show you exactly the same Array as myArray
//
const numbers = [2, 13, 4, 4, 2, 13, 13, 4, 4, 5, 5, 6, 6, 7, 5, 32, 13, 4, 5];
console.log([...new Set(numbers)]); // [2, 13, 4, 5, 6, 7, 32]
// (set "F" "f" )
new Set("Firefox"); // Set(7) [ "F", "i", "r", "e", "f", "o", "x" ]
// ("f" 2 , set )
new Set("firefox"); // Set(6) [ "f", "i", "r", "e", "o", "x" ]
const array = Array.from(document.querySelectorAll("[id]")).map((e) => e.id);
const set = new Set(array);
console.assert(set.size === array.length);
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-set-objects |
This page was last modified on 2026 7 15 by MDN contributors.
SetSet.prototype.add()Set.prototype.clear()Set.prototype.delete()Set.prototype.difference()Set.prototype.entries()Set.prototype.forEach()Set.prototype.has()Set.prototype.intersection()Set.prototype.isDisjointFrom()Set.prototype.isSubsetOf()Set.prototype.isSupersetOf()Set.prototype.keys()Set.prototype.symmetricDifference()Set.prototype.union()Set.prototype.values()Set.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |