UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ Set/ Set.prototype.clear()

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

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("bar")); // false

Specifications

Browser compatibility

See also