JavaScript Set clear() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Instance method

Definition

set.clear() removes every value from a Set, so its size becomes 0. It is the quickest way to empty a Set and start again.

The Set object itself is not replaced. It is the same object as before, only empty. So every variable that points to the Set sees the change. The method returns undefined.

Remember
set.clear()

before  → set.size is 3 (for example)
after   → set.size is 0
returns → undefined

Same Set object, now empty
Other variables that point to it see the change

Syntax

Call it on a Set. It takes no arguments.

JavaScript
const tags = new Set(["js", "css", "html"]);
console.log(tags.size);    // 3

tags.clear();
console.log(tags.size);    // 0
console.log(tags.has("js"));   // false
CallWhat it does
set.clear()Remove all values. The Set stays usable and you can add new values later.
set = new Set()Replace the variable with a brand-new empty Set. Other variables that point to the old Set still see the old values.
set.delete(value)Remove just one value instead of all of them.
set.sizeRead the number of values. It is 0 after clear().

A standard JavaScript method (ES2015) on every Set, from Set.prototype. No import required. Siblings: add(), delete(), difference(), entries(), forEach(), has(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), symmetricDifference(), union(), values(), [Symbol.iterator]().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
Set.prototype.clear()

// returns: undefined

Parameters

ParameterDescription
(none)This method takes no parameters. Call it as set.clear().

Return Value

Return ValueDescription
undefinedNothing is returned. The Set itself changes: read set.size to see that it is now 0.

JavaScript Examples

Example 1 — Empty a Set

Create a Set with three tags, empty it, and look at the size, the return value and a lookup.

JavaScript
const tags = new Set(["js", "css", "html"]);
console.log("Before: " + tags.size);

const result = tags.clear();

console.log("After: " + tags.size);
console.log("Returned: " + result);
console.log("Has js: " + tags.has("js"));
Try It Yourself

How It Works

  1. Start with values. The Set is created from an array with 3 different tags, so its size is 3.
  2. Clear it. tags.clear() removes everything. It returns undefined, so result prints as undefined.
  3. Nothing is left. The size is 0 and has("js") is false, because the old values are gone.

Example 2 — Keep the Same Set Object

Two variables point to one Set. Clear it through one variable and add a new value, then read it through the other one.

JavaScript
const cache = new Set(["a", "b"]);
const sameCache = cache;

cache.clear();
cache.add("c");

console.log("Items: " + Array.from(sameCache).join(", "));
console.log("Size: " + sameCache.size);
console.log("Same object: " + (cache === sameCache));
Try It Yourself

How It Works

  1. Two names, one Set. sameCache is not a copy. Both variables hold the same Set object.
  2. Clear and refill. clear() removes "a" and "b", and the Set is still the same object, so add("c") works on it right away.
  3. Both variables agree. Reading through sameCache shows only c, and === confirms that it is still the very same Set.

Example 3 — Clear a Set While Looping

Call clear() inside a forEach() loop and see that the loop stops, because nothing is left to visit.

JavaScript
const queue = new Set([1, 2, 3, 4]);
const seen = [];

queue.forEach((n) => {
  seen.push(n);
  if (n === 2) {
    queue.clear();
  }
});

console.log("Visited: " + seen.join(", "));
console.log("Size after: " + queue.size);
Try It Yourself

How It Works

  1. Visit in order. forEach() gives the callback 1, then 2, and the code saves each number in seen.
  2. Clear in the middle. When n is 2, queue.clear() removes all values, including 3 and 4 that were not visited yet.
  3. The loop ends early. A Set only visits values that are still inside, so 3 and 4 are never reached and the final size is 0.

Browser & Runtime Support

Set.prototype.clear() is part of ES2015 (ES6), together with Set itself. Every current browser and Node.js has it.

✓ ES2015 · Widely supported

set.clear()

Supported in Chrome 38+, Edge 12+, Firefox 37+, Safari 9+, Opera 25+ and Node.js 4+. Internet Explorer 11 only has a basic Set.

ES2015 Standard since
Google Chrome 38+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium based
Full support
Mozilla Firefox 37+ · Desktop & Mobile
Full support
Apple Safari 9+ · macOS & iOS
Full support
Opera 25+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 38+ · Chromium based
Supported
Node.js 4+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer IE 11 · basic Set only
Partial support
set.clear() Excellent

Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer needs special care.

Frequently Asked Questions

It removes every value from a Set, so the size becomes 0. The Set object itself stays the same and can be used again. The method returns undefined.
clear() empties the same Set object, so every variable that points to it sees an empty Set. set = new Set() only points one variable at a new empty Set, and other variables keep the old one with its values.
No. The values are removed for good. If you may need them again, make a copy first with const backup = new Set(set), and use the copy later.
No. It returns undefined. To check the result, read set.size, which is 0 after the call.
It is shorter and clearer, and it removes everything in a single call. For a normal-sized Set the speed difference does not matter, so choose clear() because it says exactly what you mean.

Next: delete()

Learn how to remove one value from a Set, and how the true or false result tells you if it was there.

delete() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful