How do I remove a property from a JavaScript object?

In this tutorial you'll learn how to remove a property from an object in JavaScript?
Submitted by Pratishtha Saxena, on May 16, 2022

Object in JavaScript is a variable or a variable with some properties in it. An object can be created with figure brackets {...} with an optional list of properties. A property is a "key: value" pair, where the key is a string (also called a "property name"), and the value can be anything.

var person ={
    'name':'John',
    'age': 25,
    'place':'Delhi',
    'position':'Developer'
};

In the above example, a person is an object with properties like name, age, place, and position.

Suppose we want to remove the position property of the object person.

Here's a way to do this.

JavaScript Delete Operator

This helps to delete/ remove any property of an object in JavaScript. There are two ways to write down the delete operator.

  1. Using the dot (.) Operator
    delete object.property;
    
  2. Using the square brackets []
    delete object['property'];
    

Let's understand this better by taking an example.

As mentioned above, there is an object person with properties like name, age, place, and position. Now, to remove the position from the object:

Example 1:

var person = {
	'name': 'John',
	'age': 25,
	'place': 'Delhi',
	'position': 'Developer'
};

delete person.position; // position property deleted
console.log(person);

Output:

{name: 'John', age: 25, place: 'Delhi'}

Let's also see an example of removing the property by using the square brackets.

Example 2:

var person = {
	'name': 'John',
	'age': 25,
	'place': 'Delhi',
	'position': 'Developer'
};

delete person['age']; // age property deleted
console.log(person);

Output:

{name: 'John', place: 'Delhi', position: 'Developer'}

JavaScript Examples »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.