Set.prototype.clear()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The clear() method of Set instances removes all elements from this set.

Try it

Syntax

js
clear()

Parameters

None.

Return value

None (undefined).

Examples

Using the clear() method

js
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

Specification
ECMAScript Language Specification
# sec-set.prototype.clear

Browser compatibility

desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
Deno
Node.js
clear

See also