What is the use of Delete method in Sets in JavaScript ?
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]); // Output: Set { 1, 2, 3, 4, 5 } console.log(mySet); // Delete element with value 3 from the Set let deletionResult = mySet. delete (3); // Output: true (indicating successful deletion) console.log(deletionResult); // Output: Set { 1, 2, 4, 5 } console.log(mySet); |
Output
Set(5) { 1, 2, 3, 4, 5 } true Set(4) { 1, 2, 4, 5 }
Contact Us