JavaScript

How to remove properties from objects in JavaScript

April 10, 2022

How to remove properties from objects in JavaScript

In this article you will learn how to remove properties from objects in JavaScript – using an instruction from the language itself

remove properties from objects in JavaScript thumb

Hey you programmer, ok? Let’s learn more about JavaScript and its objects!

The cool part about removing a property on an object in JavaScript is that it’s so easy

However, it is different from other forms of making changes in a language object, which most of the time it is through a method

For the remove method we will use the expression delete next to the property of the object in question

See an example:

let person = {
 name: 'Matheus',
 age: 29
}
delete person.age;
console.log(person);

The return will be:

{
 name: "Matheus"
}

Notice that the age property was deleted from the person object, after using the delete statement

We can use the other variation of accessing properties on objects:

delete object['property']

The end result is the same, just a matter of accessing the property in another way.

And a curiosity: the fact of deleting a property with delete does not free the memory used by the program

We just have the property removed from the object we want to change

Conclusion

In this article we learned how to remove properties from objects in JavaScript

For this purpose, we use the delete statement added to the property that we want to remove from the object.

So it will be deleted after the instruction, and its value, if you try to access it, will be undefined

Do you want to learn more about JS? Click here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x