Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove all the elements from a set in javascript?
The Set class in JavaScript provides a clear method to remove all elements from a given set object. This method can be used as follows −
Example
let mySet = new Set();
mySet.add(1);
mySet.add(2);
mySet.add(1);
mySet.add(3);
mySet.add("a");
console.log(mySet)
mySet.clear();
console.log(mySet)
Output
Set { 1, 2, 3, 'a' }
Set { }
You can also individually remove the elements by iterating over them.
Example
let mySet = new Set();
mySet.add(1);
mySet.add(2);
mySet.add(1);
mySet.add(3);
mySet.add("a");
console.log(mySet)
for(let i of mySet) {
console.log(i)
mySet.delete(i)
}
console.log(mySet)
Output
Set { 1, 2, 3, 'a' }
Set { }Advertisements