Open In App

Difference between Set.clear() and Set.delete() Methods in JavaScript

Last Updated : 14 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Both Set.clear() and Set.delete() methods modify the present Set by removing elements. But there are some fundamental differences between these two, which will be discussed in this article. 

Set.clear() Method: In this method, all the elements in the set get deleted. So, as a result, the set becomes empty and the size of the set becomes 0.

Syntax:

 Myset.clear()

Example: In this example, we will implement the clear() method.

Javascript




<script>
    let Myset = new Set()
    Myset.add(2)
    Myset.add(3)
    Myset.add(5)
    console.log(Myset.size)
    Myset.delete(2)
    console.log(Myset.size)
    Myset.add(2)
    Myset.clear()
    console.log(Myset.size)
</script>


Output:

3
2
0

Set.delete() Method: In this method, a value is taken as a parameter. If the Set holds the value, the element will get deleted and it will return true. If there exists no such element in the set, nothing will be removed and the function will return false. The rest of the elements will remain unaffected. So, in the other words, the delete() method deletes the element with a given value if it exists in the set and it confirms its availability in the set.  

Syntax:

Myset.delete(Value)

Example: In this example, we will implement the delete() method.

Javascript




<script>
    let Myset = new Set()
    Myset.add(2)
    Myset.add(3)
    Myset.add(5)
    console.log(Myset.size)
    Myset.delete(2)
    console.log(Myset.size)
</script>


Output:

3
2

Difference between set.clear() and set.delete() methods:

   Base of difference set.clear() method set.delete() method
Modification in Set clear() method removes all elements from the Set. delete() method removes a certain element(s) from the Set depending upon its availability.
Arguments clear() method is called without passing anything as an argument. The value of the element intended to be deleted must be passed to an argument to the delete() method.
Return type clear() method returns “undefined”.elements delete() method returns the availability of the given element in the Set. If the element intended to get deleted does not exist in the Set. It returns false otherwise returns true.
Usage clear() method is used to clear out all elements from the set. delete() method is used to delete certain element(s) from the set. As it confirms the availability of the element by returning a Boolean value. It can also be used to check whether the element exists in the set or not.
syntax  Its syntax is Myset.clear() Its syntax is Myset.delete(Argument)
Parameters This method does not accept any parameters. This method accepts one value as a parameter
Supported Browser   

Its supported browsers are -:

Google Chrome, Firefox, Internet Explorer, Opera, Edge

Its supported browsers are -:

Chrome 38 , Edge12 , Firefox 24 , Safari 8 , Opera 25 etc



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads