How to remove a Key/Property from an object in JavaScript18 Mar 2025 | 4 min read Introduction:JavaScript objects are made up of properties, which are just key-value pairs. In essence, properties consist of a pair of keys and values that can represent any type of entity. Even functions have the ability to function as a value or a key in any object. Anyway, in this section, we'll talk about how to delete keys from Object JavaScript. A key's associated value is automatically deleted when the key is removed. So, eliminating a key merely entails eliminating that attribute. There are now three ways in JavaScript to remove keys from objects. Let's take a quick look at each one. If necessary, properties in JavaScript objects can be erased. There are three simple methods we can use to quickly erase any object property. The first technique makes use of the delete operator, a particular operator in JavaScript, the second method makes use of Object Destructuring, and the third method makes use of the Reflect.deleteProperty() method. 1. Remove a Property from a JavaScript Object Using the Delete Operator:The delete operator is a specific operator in JavaScript. If we want to remove a key from an Object JavaScript, use the delete operator. The delete operator does exactly what its name implies: it removes the supplied property from the object. Although, we must first gain access to the property to delete it. Either the dot property accessor or the square brackets property accessor can be used to access the Object property. Delete with the Dot Method:Syntax: Example: output: { name: "saswat", age: 26, designation: "Software Engineer" } age: 26 designation: "Software Engineer" name: "saswat" Let's remove the age property in a dot-based manner. Output: { name: "saswat", designation: "Software Engineer" } designation: "Software Engineer" name: "saswat" Explanation: We can see from the output above that the delete operator was effective in deleting the object emp's property age (dot way). Delete with the square bracket method:Syntax: Let's use square brackets to delete the designation property from the same object emp. Output: { name: "saswat", age: 26 } name: "saswat" age: 26 Explanation: We can see from the output above that the delete operator was successful in deleting the object emp's property designation (square brackets way). 2. Remove a Property from a JavaScript Object Using Object Destructuring:Object destructuring can also be used to remove a property from an object, but there's a catch. Instead of altering the original object, a new object that doesn't include the deleted property is created. In this manner, the original item is preserved (does not change). Syntax: Example: Output: { brand: "HP", year: 2020 } { brand: "HP", model: "Notebook", year: 2020 } Explanation: In the aforementioned example, we removed a property from an object using object destructuring. When we used object destructuring to remove the property model from the original object laptop, we obtained the new object myLaptop without that property. Once more, the output demonstrates that the original item remained unchanged. 3. Remove a Property from a JavaScript Object Using the Reflect.deleteProperty()Method:The Reflect.deleteProperty() method is provided by one of the built-in JavaScript objects called 'Reflect'. This approach is analogous to the delete operator's function form, which we have discussed. Example: Output: { car1: 'Honda', car3: 'Toyota' } Explanation: In the example above, there was an object called "cars" that had three properties. To remove the car2 property from that object, we used the Reflect.deleteProperty() method. The output demonstrates that deleting the car2 property from the cars object was successful. Finally, we'd like to say that it makes no difference whether you use the delete operator or the Reflect.deleteProperty() method because they both do the work with essentially the same temporal complexity. However, utilising Object Destructing takes more time. Hence, due to its simple syntax and low time complexity, the delete operator is the most popular method of removing keys from javascript objects. ConclusionJavaScript objects are made up of properties, which are just key-value pairs. A key's associated value is automatically deleted when the key is removed. Next TopicJavaScript Endswith() Function |
? The Promise in JavaScript may look quite complicated to understand at first sight, but in reality, it is quite simple and is not rocket science. In JavaScript, a promise is just like a promise that you make in real life to show that you are...
4 min read
JavaScript allows us to add special characters to a text String using a backslash (\) sign. We can add different types of special characters, including the single quote, double quote, ampersand, new line, tab, backspace, form feed, etc., using the backslash just before the characters. The...
2 min read
JavaScript is a potent scripting language that gives programmers the ability to make dynamic, interactive web sites. Finding a specific element's parent element is a frequent operation when working with the Document Object Model (DOM). Here, we'll look at a variety of JavaScript-based approaches to accomplishing...
6 min read
The querySelector is a JavaScript method that plays a vital role in the searching of elements. In this section, we will understand and discuss the querySelector () method, its use and also look over an example to understand the concept of the querySelector () method practically. Introducing...
5 min read
We basically face some type of situation during programming in javascript in which we can create any function on the button click. For example, we have to examine some more functionality in the time of automation testing of a web page or a site. In...
3 min read
? JavaScript offers three in-built functions to add or insert objects in an array. These methods are - push() splice() unshift() You can use any of these methods to add the objects to the array. Objects are the elements or values in the array. We will discuss each method in...
9 min read
The appendChild() is a method of the Node interface in JavaScript. The appendChild() method enables us to add a node to the end of a specified parent node's child node list. Syntax: The syntax of the appendchild() method is: parentNode.appendChild(childNode) In the syntax, the childNode is the node that...
3 min read
Blobs are immutable objects that represent unprocessed data. The file is a Blob derived from data from the file system. Blobs allow us to create file-like objects on the client that we may transfer to APIs, and these APIs expect URLs rather than needing the...
5 min read
In this article, we are going to discuss currying in JavaScript. Here, we will see how currying works and how it will be useful for software developers. We will also see the conversion of an existing function into the curried version. So, this article is...
4 min read
A 2D drawing context's translate() method is used. The canvas and its origin are moved by x and y units using the translate(x,y) method. Otherhand, The drawing position (0,0) on the canvas is remapped by the translate() method. The value is included in the x- and...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India