
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 { }
- Related Articles
- How to remove all the elements from a map in JavaScript?
- Remove elements from a Set using Javascript
- Java Program to remove all elements from a set in Java
- How to remove all elements from a Collection in another Collection
- Remove all elements from a HashSet in Java
- Remove all elements from a HashSet in C#
- Remove all elements from a SortedList in C#
- Remove all elements from the ArrayList in Java
- Remove all elements from the ArrayList in C#
- Remove all elements from the SortedSet in C#
- Remove all elements from the Collection in C#
- Remove all elements from the Hashtable in C#
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- How to remove duplicate elements from an array in JavaScript?
- Remove all elements from TreeSet in Java

Advertisements