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.

La méthode clear() permet de retirer tous les éléments d'un ensemble Set.

Exemple interactif

const set1 = new Set();
set1.add(1);
set1.add("foo");

console.log(set1.size);
// Expected output: 2

set1.clear();

console.log(set1.size);
// Expected output: 0

Syntaxe

js
monSet.clear();

Valeur de retour

Exemples

js
var monSet = new Set();
monSet.add(1);
monSet.add("toto");

monSet.size; // 2
monSet.has("toto"); // true

monSet.clear();

monSet.size; // 0
monSet.has("truc"); // false

Spécifications

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

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
clear

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Voir aussi