| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/clear | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since July 2015.
The clear() method of Set instances removes all elements from this set.
const set = new Set();
set.add(1);
set.add("foo");
console.log(set.size);
// Expected output: 2
set.clear();
console.log(set.size);
// Expected output: 0
clear()
None.
None (undefined).
const mySet = new Set();
mySet.add(1);
mySet.add("foo");
console.log(mySet.size); // 2
console.log(mySet.has("foo")); // true
mySet.clear();
console.log(mySet.size); // 0
console.log(mySet.has("foo")); // false
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-set.prototype.clear |
This page was last modified on Jul 20, 2025 by MDN contributors.
SetObject/FunctionYour 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 |