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.
- The delete keyword removes a property from an object.
- The delete keyword deletes both the property's value and the property itself.
- After deletion, the property cannot be used before adding it back again.
- 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.
Leave your comment