Guvi-blog-logo

How To Remove A Property From A JavaScript Object? [EXPLAINED]

how to remove a property from a JavaScript Object

JavaScript is somewhat similar to Java, and it follows some of the OOPs concepts. One such concept is the “instance”. Before we step into today’s topic: ‘how to remove a property from a JavaScript object’, let me quickly brief you about the JavaScript objects.

JavaScript Object

An object is a data type, a non-primitive one. It works identical to other JS variables, but there are some differences between a normal JavaScript variable and JS Object.

Variables usually store a single property, whereas JS Object holds multiple property values. These property values can be some information about primitive data types and different methods.

Since JavaScript doesn’t have any concept of Classes, therefore, establishing functionalities similar to a class, Objects can be modified. That modification includes creating functions. With the combination of functions and objects, the functionality of the class can be achieved.

Read more on: How To Use “use strict” String For Strict Mode In JavaScript?

Now, let’s have quickly have a look on:

How to Create an Object with properties

JavaScripts offers two different ways to implement an Object with properties in the script:

→ Object Literal

→ Object Constructor

Object Literal

In this method, we follow the basic step of a variable declaration, but the only difference is that here there will be ‘{}’ block for properties. Refer to the illustration below:

In the above image, you can see that an Object named “demoobj” is used using the var keyword, and in the “{ }” block, some properties are created.

These properties can also include a function.

Object Constructor

If you have tried your hands on other object-oriented programming languages, you would have used a way similar to the following to create an object:

But now, you might be wondering where the properties are. You need to call the object and create a property using the dot “.” operator. For instance,

So, that was a brief about JavaScript objects and different methods of creating an object. Now, let’s focus on today’s topics, i.e., “How to delete properties from a JavaScript object”?

Here, I don’t think it is required to explain what properties mean again.

Quick Recap, the property can be a variable or a method that the object is holding. For instance, in the above object, i.e., demoobj, size is a property. In the next section of this guide, we will learn how to delete the size property from the object.

Read more here: The Beginner’s Guide To Closures In Javascript

How to delete a property from a JavaScript Object. [With Example]

In order to delete a property from an Object of JavaScript, you need to use a particular keyword. That keyword is “delete”. Use delete followed by the object name dot property name.

Now, this keyword will delete the property’s value and delete the property itself. Therefore, if you want to add the property again, you can’t just assign a value. You have to create a new property using the traditional method (discussed above).

The “delete” keyword returns a boolean value, and It returns True if the process of deleting the property gets successfully executed, false if the property doesn’t get deleted.

Points to be remembered:

  • If you try to delete a particular property that never existed, the delete statement will be executed, but it won’t affect the object, and the delete keyword will return True.
  • delete” can’t delete the global scope properties.
  • Properties declared with the keyword let, or const can’t be deleted in their local scope.
  • Properties that are Non-Configurable in nature can’t be deleted using the delete keyword.

Final Notes:

So, by now you might have an idea of how to remove a property from a JavaScript object?

If you want to master the delete keyword, it is recommended to experiment with it in your practice programs. Gradually, after regress debugging, you’ll have a clear picture of the delete keywords working.

Besides deleting a property of a JavaScript Object, the delete keyword have several other applications. Now, your task, for now, is to find those applications and drop them down in the comments.

MCQs:

#1. Which keyword is used to delete a property of Object?

  1. return
  2. delete
  3. var
  4. remove

Answer: b

#2. Which of the following languages have the concept of delete?

  1. Python 
  2. JavaScript 
  3. C/C++ 
  4. All of the above 

Answer: d

#3. Does the delete statement return a value?

  1. Yes 
  2. No 
  3. Could be
  4. Only when the operation is successful.

Answer: a

#4. Which of the following syntax is correct?

  1. delete object propertyName 
  2. delete object. property 
  3. Both a & b 
  4. None of the above

Answer: b

#5. Can a delete keyword remove an object? 

  1. Yes, it can, but its property remains as it is. 
  2. Yes, but its properties are also deleted. 
  3. No 
  4. Maybe. 

Answer: 3

So, that was all about: how to remove a property from a JavaScript object? Leave us your queries and suggestions below. And if you wish to take expert advice regarding your career or discuss some tech-related query with us, kindly leave us your contact, we will get back to you shortly.

Contact Form

By clicking 'Submit' you Agree to Guvi Terms & Conditions.

Our Learners Work at

Our Popular Course

Share this post

Author Bio

Archana
Archana
A traveler, and explorer, Archana is an active writer at GUVI. You can usually find her with a book/kindle binge-reading (with an affinity to the mystery/humor).

Our Live Classes

Learn Javascript, HTML, CSS, Java, Data Structure, MongoDB & more
Learn Python, Machine Learning, NLP, Tableau, PowerBI & more
Learn Selenium, Python, Java, Jenkins, Jmeter, API Testing & more
Learn Networking, Security Testing, IAM, Access Management & more

Hey wait, Don’t miss New Updates from GUVI!

Get Your Course Now

Related Articles