Q:

How to Remove a Property from a JavaScript Object

0

How to Remove a Property from a JavaScript Object

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the delete Operator

You can use the delete operator to completely remove the properties from the JavaScript object. Deleting is the only way to actually remove a property from an object.

Setting the property to undefined or null only changes the value of the property. It does not remove property from the object. Let's take a look at the following example:

<script>
    var person = {
        name: "Harry",
        age: 16,
        gender: "Male"
    };
    
    // Deleting a property completely
    delete person.age;
    console.log(person.age);  // Prints: undefined
    console.log(person);  // Prints: {name: "Harry", gender: "Male"}
    
    // Setting the property value to undefined
    person.gender = undefined;
    console.log(person.gender);  // Prints: undefined
    console.log(person);  // Prints: {name: "Harry", gender: undefined}
</script>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Check If a Value Exists in an Array in Java... >>
<< How to Remove Duplicate Values from a JavaScript A...