The Wayback Machine - https://web.archive.org/web/20241008202919/https://www.geeksforgeeks.org/javascript-set-delete-method/
Open In App

JavaScript Set delete() Method

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The delete() method in JavaScript Set deletes an element from the set and returns true if the element existed and was successfully deleted. Otherwise, it returns false if the element does not exist.

Syntax:

mySet.delete(value);

Parameters:

  • value: It is the value of the element that is to be deleted from the Set.

Return value: It will return true if the value was present in the set and false if the value did not exist after removing the element.

Example 1: Removing Elements from a Set with the delete() Method

This code creates a new set called myset and adds two elements (75 and 12) using the add() method. Then, it prints the modified set. After that, it uses the delete() method to remove 75 from the set and prints the set again.

JavaScript
// Create a new set using Set() constructor
let myset = new Set();

// Append new elements to the set
// using add() method
myset.add(75);
myset.add(12);

// Print the modified set
console.log(myset);

// As 75 exists, it will be removed 
// and it will return true
console.log(myset.delete(75));
console.log(myset);

Output
Set(2) { 75, 12 }
true
Set(1) { 12 }

Example 2: Deleting Non-Existent Elements from a Set with the delete() Method

The code initializes a Set, adds elements, attempts to delete a non-existent element (43), which returns false. The Set remains unchanged, containing [23, 12].

JavaScript
// Create a new set using Set() constructor
let myset = new Set();

// Append new elements to the set
// using add() method
myset.add(23);
myset.add(12);

// Print the modified set
console.log(myset);

// As 43 does not exist nothing will be 
// changed and it will return false
console.log(myset.delete(43));
console.log(myset);

Output
Set(2) { 23, 12 }
false
Set(2) { 23, 12 }

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

JavaScript Set delete() Method- FAQs

What does the delete() method do?

The delete() method removes a specified element from a Set object. If the element is found and successfully removed, it returns true; otherwise, it returns false.

How does delete() affect the references to the elements in the set?

The delete() method removes the reference to the specified element in the set, but it does not affect external references to that element.

How does delete() handle objects and arrays?

delete() removes objects and arrays based on their reference. The exact same instance must be passed to delete() for it to remove the element.

What are some practical applications of the delete() method?

Practical applications of the delete() method include dynamically removing specific elements from a collection, ensuring uniqueness in a set by removing duplicates before adding new elements, and managing a set of items where elements may need to be removed based on user actions or other conditions.

What is the most common use of the Set.prototype.delete() method?

Most Common Use Cases:

  • Removing a specific item from a set when it is no longer needed.
  • Dynamically managing the contents of a set based on certain conditions.
  • Maintaining a collection of unique items by removing duplicates.
  • Modifying the set during operations such as filtering or updating based on specific criteria.

We have a complete list of Javascript Set methods, to check those please go through this Sets in JavaScript article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  


Previous Article
Next Article

Similar Reads

Which one method use to delete an element from JavaScript array (slice or delete) ?
We can use both methods to delete an element from a javascript array. The answer is solely dependent on the application and on the programmer. What slice() method does? The slice method, as the name suggests, slices an array and returns a copy of the new array. This method does not affect the main array, it just returns the copy of the array. We ca
4 min read
Delete the array elements in JavaScript | delete vs splice
This article will show you how to Delete the array element at a specific position using JavaScript. There are two approaches that can be used to delete elements in an array. They have their own merits regarding the way they perform the deletion. Delete array[index]:This method deletes the element at the index specified but does not modify the array
3 min read
Difference between Set.clear() and Set.delete() Methods in JavaScript
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: M
2 min read
JavaScript weakMap delete() Method
The weakMap.delete() is an inbuilt function in JavaScript which is used to delete a particular element from an object WeakMap. Syntax: weakMap.delete(key); Parameters: It accepts a parameter "key" which is the key of the element which is going to be deleted from the object weakMap. Return values: It returns true if that element has been deleted fro
2 min read
JavaScript weakSet delete() Method
JavaScript weakSet.delete() method is used to delete a specific element from a weakSet object. The WeakSet object lets you store weakly held objects in a collection. Syntax: weakSet.delete(value); Parameters: This method accepts a single parameter value. value: This value will be deleted from the weakset object. Return Values: It returns true if th
2 min read
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 del
1 min read
Delete the first element of array without using shift() method in JavaScript
Given an array containing some array elements and the task is to remove the first element from the array and thus reduce the size by 1. We are going to perform shift() method operation without actually using it with the help of JavaScript. There are two approaches that are discussed below: Table of Content Method 1: Using splice() MethodMethod 2: U
2 min read
JavaScript Map delete() Method
JavaScript Map.delete() method is used to delete the specified element among all the elements that are present in the map. The Map.delete() method takes the key that needs to be removed from the map, thus removing the element associated with that key and returning true. If the key is not present then it returns false. Syntax:my_map.delete(key);Para
3 min read
Difference between PUT and DELETE request in Vanilla JavaScript
PUT and DELETE are the two different types of HTTP request methods. HTTP protocol supports many methods to transfer data from the server or do any operation on the server. The HTTP protocol supports the following methods, e.g. GET, POST, PUT, DELETE, PATCH, COPY, HEAD, OPTIONS, etc. Before we dive into the main difference between PUT and DELETE req
3 min read
JavaScript SyntaxError - Applying the 'delete' operator to an unqualified name is deprecated
This JavaScript exception applying the 'delete' operator to an unqualified name is deprecated works in strict mode and it occurs if variables are tried to be deleted with the delete operator. Message: SyntaxError: Calling delete on expression not allowed in strict mode (Edge) SyntaxError: applying the 'delete' operator to an unqualified name is dep
1 min read
JavaScript TypeError - Can't delete non-configurable array element
This JavaScript exception can't delete non-configurable array element that occurs if there is an attempt to short array-length, and any one of the array's elements is non-configurable. Message: TypeError: can't delete non-configurable array element (Firefox) TypeError: Cannot delete property '2' of [object Array] (Chrome) Error Type: TypeError Caus
1 min read
Delete Operations in Doubly Linked List using JavaScript
This article will demonstrate how to perform delete operations in a Doubly Linked List Data Structure using JavaScript. We will see different approaches to deleting elements from the Doubly Linked List. Steps to Delete Element Node from Doubly Linked ListA doubly linked list has two pointers that link it with the previous and next node. So if we ha
11 min read
JavaScript Program to Delete Property from Spread Operator
This article will demonstrate how to delete property from the spread operator in JavaScript. In JavaScript, the spread operator allows to create a shallow copy of an objеct or mеrgе propеrtiеs from one objеct into another without modifying the original objеct. Table of ContentUsing JavaScript object destructuring Using the rest syntax with object d
2 min read
How to create Delete Modal Card using JavaScript and Tailwind ?
Create a delete confirmation modal with Tailwind CSS, a utility-first framework for building modern user interfaces. This modal features customizable button styling, modal content layout, and success message display, providing a seamless user experience. Utilize Tailwind's classes to effortlessly design and enhance UI components. Output Preview: Le
2 min read
JavaScript Program to Delete Middle of Doubly Linked List
We are given a doubly linked list and we have to write a JavaScript program to delete the middle node of the list and print the newly formed linked list after the deletion. Example: Input: 1 <-> 2 <-> 3 <-> 4 <-> 5Output: 1 <-> 2 <-> 4 <-> 5Below are the different approaches to delete the middle of a doubly
4 min read
Different ways to delete an item from an array using JavaScript
In Javascript, we do not have any array.remove() method for deleting the element. we will have an array and we need to delete a given item from that array and return the resulting array in the console. These are the following methods for solving this problem: Table of Content Using for loop and push() MethodUsing Pop() MethodUsing shift() MethodUsi
4 min read
JavaScript Program to Delete Middle Element from an Array
In this article, we will learn different approaches to how we can delete the middle element from a given array. We will use these JavaScript methods to achieve this: Methods to Delete Middle Element from an ArrayTable of Content Methods to Delete Middle Element from an ArrayUsing JavaScript Spread OperatorUsing the JavaScript splice() methodUsing t
4 min read
Delete Without Head Pointer using JavaScript
The objective is to remove a node in a JavaScript singly linked list without having a direct reference to the head node. This entails making changes to the node that will be removed without actually using the head pointer. Below are the approaches to deleting a node without a head pointer using JavaScript: Table of Content Copy Data and Adjust Poin
6 min read
JavaScript Program to Delete an Image by Clicking on a Button
In this article, we will learn how to delete an image by clicking on a button in JavaScript. In web development, there are scenarios where you may want to provide users with the ability to delete an image by clicking on a button. This can be useful in various applications, such as image galleries or user profile settings. ApproachThe approach to de
2 min read
JavaScript Program to Delete all Nodes of a Binary Search Tree
A Binary Search Tree (BST) is a type of binary tree where each node has at most two children referred to as the left child and the right child. The value of the left child node is less than the value of the current node and the value of the right child node is greater than the value of the current node in the binary search tree. We will insert node
6 min read
How to Add Edit and Delete Data in HTML Table using JavaScript?
In web development, it is often necessary to handle data within an HTML table. JavaScript offers robust capabilities for dynamically incorporating, modifying, and removing data within a table. These functionalities enable the manipulation of table content without the need to refresh the entire page, resulting in seamless and effective user interact
4 min read
How to delete an element from an array using JavaScript ?
The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index. In this article, we will discuss different ways to remove elements from the array. There are many methods that are used to remove el
5 min read
Javascript Program To Delete Middle Of Linked List
Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if given linked list is 1->2-
3 min read
Javascript Program To Delete N Nodes After M Nodes Of A Linked List
Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list. Difficulty Level: Rookie Examples: Input:M = 2, N = 2Linked List: 1->2->3->4->5->6->7->8Output:Linked List: 1->2->5->6Input:M = 3, N = 2Linked List
3 min read
Javascript Program To Delete Nodes Which Have A Greater Value On Right Side
Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: Input: 12->15->10->11->5->6->2->3->NULL Output: 15->11->6->3->NULL Explanation: 12, 10, 5 and 2 have been deleted because there is a greater value on the right side. When we examine 12, we see that after 12 th
5 min read
Javascript Program To Delete Alternate Nodes Of A Linked List
Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Method 1 (Iterative):Keep track of the previous node tha
4 min read
Javascript Program For Writing A Function To Delete A Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. This article focuses on writing a function to delete a linked list. Implementation: [GFGTABS] JavaScript // Javascript program to delete // a linked list // Head of the list let head
2 min read
Delete first character of a string in JavaScript
Delete the first character of a string in JavaScript refers to removing the initial character from a given string and returning the rest of the string. Below are the methods used to delete the first character of a string in JavaScript: Table of Content Using slice() Method Using substr() MethodUsing the substring() Method Using slice() Method The s
2 min read
JavaScript delete Operator
The delete operator in JavaScript removes properties from objects, including inherited ones, and creates holes in arrays without changing their length. If a deleted property holds an object with no other references, that object is automatically released by JavaScript's garbage collector over time. Syntax delete object // or delete object.property /
3 min read
ASP Delete Method
The ASP File.Delete Method is used to delete a particular file or folder from a system. Syntax For file object:FileObject.Delete[(force)]For folder object: FileObject.Delete[(force)] Parameter Values: force: It is an optional attribute. It contains a Boolean value which indicates that whether a read-only file will be deleted or not. It set to true
1 min read