Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
* Some parts of this feature may have varying levels of support.
Set 物件可讓你儲存任何類型的唯一值(unique),不論是基本型別(primitive)值或物件參考(references)。
若一個可迭代物件被傳入,其所有的元素將會被加入至新的 Set。若你沒有指定此參數,或參數值為 null,則新的 Set 會是空的。
一個新的 Set 物件。
Set 對象是數值的收集器。你可以按插入順序迭代收集器中的元素。在 Set 裡的元素只會出現一次;意即在 Set 裡的元素都是獨一無二
因為在 Set 裡每個值都是獨立的,所以都會檢查值的相等性。在早期的 ECMAScript 規範版本中,此處算法跟基於===操作符中使用的算法並不相同。具體來說,在 Set 裡 +0(在嚴格模式是和 -0 相等)和 -0 是不同的值。然而在 ECMAScript 2015 規範中這點已被更改。請參閱瀏覽器相容性中的「Value equality for -0 and 0」。
另外,NaN 和 undefined 都可以被放置在 Set 中, NaN 之間被視為相同的值(儘管 NaN !== NaN)。
Set.lengthThe value of the length property is 0.
Set[Symbol.species]The constructor function that is used to create derived objects.
Set.prototypeRepresents the prototype for the Set constructor. Allows the addition of properties to all Set objects.
All Set instances inherit from Set.prototype.
The initial value of the Symbol.toStringTag property is the string "Set". This property is used in Object.prototype.toString().
Set.prototype.sizeReturns the number of values in the Set object.
Inserts a new element with a specified value in to a Set object, if there isn't an element with the same value already in the Set.
Set.prototype.clear()Removes all elements from the Set object.
Set.prototype.delete()Removes the element associated to the value and returns a boolean asserting whether an element was successfully removed or not. Set.prototype.has(value) will return false afterwards.
Set.prototype.has()Returns a boolean asserting whether an element is present with the given value in the Set object or not.
Set.prototype[Symbol.iterator]()Returns a new iterator object that yields the values for each element in the Set object in insertion order.
Set.prototype.values()Returns a new iterator object that yields the values for each element in the Set object in insertion order.
Set.prototype.keys()An alias for Set.prototype.values().
Set.prototype.entries()Returns a new iterator object that contains an array of [value, value] for each element in the Set object, in insertion order.
This is similar to the Map object, so that each entry's key is the same as its value for a Set.
Set.prototype.forEach()Calls callbackFn once for each value present in the Set object, in insertion order. If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackFn.
| ECMAScript® 2027 Language Specification # sec-set-objects |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月14日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.