Map.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() supprime tous les éléments d'un objet Map.

Exemple interactif

const map1 = new Map();

map1.set("bar", "baz");
map1.set(1, "foo");

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

map1.clear();

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

Syntaxe

js
maMap.clear();

Valeur de retour

Exemple

Utiliser la méthode clear()

js
var maMap = new Map();
maMap.set("truc", "bidule");
maMap.set(1, "toto");

maMap.size; // 2
maMap.has("truc"); // true

maMap.clear();

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

Spécifications

Specification
ECMAScript® 2025 Language Specification
# sec-map.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