| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/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 ..
* Some parts of this feature may have varying levels of support.
"Set" - . Set ; . . , add() ( , add(), ).
, " , ". , - ( O(1)), ( O(log(N))) , , , O(N).
Set , . SameValueZero. ( SameValue, 0 -0 . ). , NaN NaN ( NaN !== NaN), ===.
has , , . , Array.prototype.includes , , .
Set() Set.
get Set[@@species]-, .
Set.prototype[@@toStringTag] @@toStringTag - "Set". Object.prototype.toString().
Set.prototype.size Set.
Set.prototype.add()
Set, Set .Set.prototype.clear()
Set.Set.prototype.delete()
Set.prototype.has(value) false.Set.prototype.has()
Set .Set.prototype[@@iterator]()
Set .Set.prototype.values()
Set .Set.prototype.keys()
Set.prototype.entries()
[value, value] Set . Map, Set (key) (value).
Set.prototype.forEach()
callbackFn , Set . thisArg, this callbackFn.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(""); // Set(3) { 1, 5, '' }
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
mySet1.has(5); // true
mySet1.has(Math.sqrt(25)); // true
mySet1.has("".toLowerCase()); // true
mySet1.has(o); // true
mySet1.size; // 5
mySet1.delete(5); // 5 set
mySet1.has(5); // false, 5
mySet1.size; // 4,
mySet1.add(5); // Set(5) { 1, "", {...}, {...}, 5 } - ,
console.log(mySet1); // Set(5) { 1, "", {}, {}, 5 }
.
for (const item of mySet1) {
console.log(item);
}
// 1, "", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5
for (const item of mySet1.keys()) {
console.log(item);
}
// 1, "", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5
for (const item of mySet1.values()) {
console.log(item);
}
// 1, "", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5
//
for (const [key, value] of mySet1.entries()) {
console.log(key);
}
// 1, "", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5
// Set Array Array.from
const myArr = Array.from(mySet1); // [1, "", {"a": 1, "b": 2}, {"a": 1, "b": 2}, 5]
// , HTML-
mySet1.add(document.body);
mySet1.has(document.querySelector("body")); // true
// Set Array
const mySet2 = new Set([1, 2, 3, 4]);
console.log(mySet2.size); // 4
console.log([...mySet2]); // [1, 2, 3, 4]
//
const intersection = new Set([...mySet1].filter((x) => mySet2.has(x)));
//
const difference = new Set([...mySet1].filter((x) => !mySet2.has(x)));
// 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
const mySet = new Set(myArray);
mySet.has("value1"); // true
// spread .
console.log([...mySet]); // , myArray
const numbers = [2, 3, 4, 4, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 5, 32, 3, 4, 5];
console.log([...new Set(numbers)]); // [2, 3, 4, 5, 6, 7, 32]
// ( "F" "f")
new Set("Firefox"); // Set(7) [ "F", "i", "r", "e", "f", "o", "x" ]
// ("f" , )
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 29 . 2026 . by MDN contributors.
SetObject/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 |