How to remove a property from a JavaScript?

Asked 19-Jul-2021
Viewed 333 times

1 Answer


0

delete operator removes a property from an object in javascript, its single operand property must be an access expression, deletes the value of not only the property but also the value.

  1. The delete keyword removes a property from an object.
  2. The delete keyword deletes both the property's value and the property itself.
  3. After deletion, the property cannot be used before adding it back again.
  4. It has no effect on the variable or function.

Syntax

delete object-name.property-name

Example 

<!DOCTYPE html>

<html>
<body>
<h2>JavaScript Object Properties</h2>
<h1 id='hh'></h1>
<h2 style='color :red'>after deleting the age property from an object</h2>
<p id='demo'></p>
<script>
var person = {
  firstname: 'John',
  lastname: 'Doe',
  age: 50,
  eyecolor: 'blue'
};
document.getElementById('hh').innerHTML =person.firstname + ' is ' + person.age + ' years old.';
delete person.age;
document.getElementById('demo').innerHTML =
person.firstname + ' is ' + person.age + ' years old.';
</script>
</body>
</html>

Output 

JavaScript Object Properties

John is 50 years old.

after deleting the age property from an object

John is undefined years old.


Loading...
Ads