What is the use of Delete method in Sets in JavaScript ?
Last Updated :
01 Feb, 2024
In JavaScript Sets, the delete()
method is used to remove a specified element from the Set. It returns a boolean value indicating whether the element was successfully deleted.
Syntax:
mySet.delete(value);
Parameters:
mySet
: The Set from which the element is to be deleted.
value
: The value of the element to be deleted from the Set.
Example: Here, the delete(3)
method call removes the element with the value 3
from the Set, and the deletionResult
variable holds true
to indicate that the deletion was successful.
Javascript
let mySet = new Set([1, 2, 3, 4, 5]);
console.log(mySet);
let deletionResult = mySet. delete (3);
console.log(deletionResult);
console.log(mySet);
|
Output
Set(5) { 1, 2, 3, 4, 5 }
true
Set(4) { 1, 2, 4, 5 }
Share your thoughts in the comments
Please Login to comment...