• JavaScript Video Tutorials

JavaScript - Set.delete() Method



The JavaScript Set.delete() method is used to delete a specified value from a set. This method returns a Boolean value as a result: "true" if the element with the specified value was found and deleted, and "false" if the element with the specified value was not found.

Syntax

Following is the syntax of JavaScript Set.delete() method −

Set.prototype.delete()

Parameters

This method accepts the following parameter −

  • value − The value of the element to be deleted from the set.

Return value

This method returns a Boolean value as a result.

Examples

Example 1

In the following example, we are using the JavaScript Set.delete() method to delete the element "Orange" from the set −

<html>
<body>
   <script>
      let fruits = new Set(['Apple', 'Banana', 'Orange']);
      document.write(fruits.delete('Orange'), "<br>");
      document.write(`Result: ${[...fruits]}`);
   </script>
</body>
</html>

If we execute the above program, it returns true because the element "Orange" is present in the set and has been removed.

Example 2

Here, we are deleting the element "Grapes" which is not present in the set −

<html>
<body>
   <script>
      let fruits = new Set(['Apple', 'Banana', 'Orange']);
      document.write(fruits.delete('Grapes'), "<br>");
      document.write(`Result: ${[...fruits]}`);
   </script>
</body>
</html>

As we can see in the output, it returns false because the element "Grapes" is present in the set.

Advertisements