The Set.clear() method in JavaScript is used for the removal of all the elements from a set and make it empty. No arguments are required to be sent as parameters to the Set.clear() method and it returns an undefined return value.
Syntax:
mySet.clear()
Parameters: This method does not any accept any parameters.
Return Value: This method returns an undefined value.
Example 1:
Javascript
let myset = new Set();
myset.add(23);
console.log(myset);
console.log(myset.size);
myset.clear();
console.log(myset.size);
|
Output:
Set(1) { 23 }
1
0
Example 2:
Javascript
let myset = new Set();
myset.add( "Manchester" );
myset.add( "London" );
myset.add( "Leeds" );
console.log(myset);
console.log(myset.size);
myset.clear();
console.log(myset.size);
|
Output:
Set(3) { 'Manchester', 'London', 'Leeds' }
3
0
Supported Browsers:
- Google Chrome: 38+
- Firefox: 19+
- Internet Explorer: 11+
- Opera: 25+
- Edge: 12+
- Safari: 08+
We have a complete list of Javascript Set methods, to check those please go through this Sets in JavaScript article.
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Jan, 2023
Like Article
Save Article