javascript instance method
Set.prototype.clear()
The clear() method of Set instances removes all elements from this set.
Interactive exampleOpen the canonical MDN source to run this embedded demo.
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
Syntax
clear()
Parameters
None.
Return value
None (undefined).
Examples
Using the clear() method
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
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.