Set.prototype.clear()

clear() 方法移除 Set 对象中所有元素。

尝试一下

语法

js

clear()

返回值

示例

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

规范

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

浏览器兼容性

BCD tables only load in the browser

参见