A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-set-clear-method/ below:

JavaScript Set clear() Method - GeeksforGeeks

JavaScript Set clear() Method

Last Updated : 23 Jul, 2025

The Set clear() method in JavaScript is used for the removal of all the elements from a set and making it empty.

Syntax:
mySet.clear();
Parameters: Return Value:

Example 1: Emptying set using Set clear() method

JavaScript
// Create a new set using Set() constructor
let myset = new Set();

// Append new elements to the
// set using add() method
myset.add(23);

// Print the modified set
console.log(myset);
console.log(myset.size);

// The clear() method will remove 
// all elements from the set 
myset.clear();

// This will return 0 as there
// are no elements present in the Set
console.log(myset.size);
Explanation:

Example 2: Emptying set using Set clear() method

JavaScript
// Create a new set using Set() constructor
let myset = new Set();

// Append new elements to the
// set using add() method
myset.add("Manchester");
myset.add("London");
myset.add("Leeds");

// Print the modified set
console.log(myset);
console.log(myset.size);

// The clear() method will remove
// all elements from the set 
myset.clear();

// This will return 0 as the set is empty
console.log(myset.size);

Output
Set(3) { 'Manchester', 'London', 'Leeds' }
3
0

Explanation:

We have a complete list of Javascript Set methods, to check those please go through this Sets in JavaScript article.

Supported Browsers:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4