Remove element from object JavaScript

In this article, we will try to understand how to add properties to an object as well as how to add or remove the properties from an object in JavaScript.

Before we actually go and see the addition and removal of properties from an object let us first understand the basics of an object in JavaScript.

Object:

  • An object in JavaScript is the collection of properties.
  • A single property in a JavaScript object is actually the association between the name (or key ) and a value.
  • An object can contain a different number of properties which is further having different names as well as values.

Syntax: By using the following syntax one can easily create an object with a different number of properties.

let object_name =
{
    property_name: value,
    ...
}

Now that we have understood the basic details associated with the object in JavaScript, let us see some examples to add properties in an object as well as how to remove properties from an object.

Adding/Removing Properties from an Object:

Remove element from object JavaScript

  • As depicted in the above pictorial representation, we can easily add or remove several properties from an object in JavaScript by following certain methods or techniques.
  • For adding any property, one could either use object_name.property_name = value (or) object_name[“property_name”] = value.
  • For deleting any property, one could easily use delete object_name.property_name (or)  delete object_name[“property_name”].

Let us see some examples in order to have a better understanding of how to add and remove properties from an object.

Example 1: In this example, we first create an object, and further we will be adding some properties to it.

How to remove a property from JavaScript object ?

Improve Article

Save Article

Like Article

  • Last Updated : 03 Jan, 2023

  • Read
  • Discuss
  • Courses
  • Practice
  • Video
  • Improve Article

    Save Article

    JavaScript delete keyword is used to delete properties of an object in javaScript. 

    Syntax:

    delete object.property or
    delete object[property]

    Note:

    • Delete keyword deletes the both value of the property and the property also. After deletion, the property can not be used.
    • The delete operator is designed to use on object properties. It can not be used on variables or functions.
    • Delete operators should not be used on predefined JavaScript object properties. It can cause problems.

    Example 1: This example deletes the address property of an object. 

    html




    <h1 style="color:green;">

        GeeksforGeeks

    </h1>

    <<3 <4=<6<7<3>

    h1

    <h12 h13=h15h16

    </h12>

    h1

    <style2>

        style5

        style7

    style8style9

    style8=1

    style8=3

        =5

        =7

        =9

        "color:green;"1

        "color:green;"3

    </style2>

    Output:

    Remove element from object JavaScript

    Remove a property from JavaScript object

    Example 2: This example deletes the age property of an object.

    HTML




    <h1 style="color:green;">

        GeeksforGeeks

    </h1>

    <<3 <4=<6    3<3>

    h1

    <h12 h13=h15>

        GeeksforGeeks4

    </h12>

    h1

    <style2>

        style5

        style7

    style8style9

    style8=1

    style8=3

        =5

    <04 

        <06

        <08

    <09<10

        "color:green;"3

    </style2>

    Output:

    Remove element from object JavaScript

    Remove a property from JavaScript object


    My Personal Notes arrow_drop_up

    Save

    Please Login to comment...

    How to remove an element from an object in JavaScript?

    Remove Property from an Object The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties.

    How to remove key value from object in JavaScript?

    The delete operator is used to delete the key-value pair where the key is “key2”. console. log(obj); The output of the above code in the console will be: { key1: "value1", key3: "value3" }.

    How to remove last element from object in JavaScript?

    Using the Array pop() Method The Array pop() method is the simplest method used to remove the last element in an array. The pop() method returns the removed element from the original array.

    How to remove first element from object in JavaScript?

    JavaScript Array shift() The shift() method removes the first item of an array.