How to remove a property from a JavaScript?

0 votes
41 views
Ravi Vishwakarma asked 7 days ago in Web Application by Ravi Vishwakarma

How to remove a property from a JavaScript?

1 Answer

1 votes
Ethan Karla answered 4 days ago by Ethan Karla
The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.
The delete operator deletes a property from an object.
<!DOCTYPE html>

<html>
<body>
<h2>JavaScript Object Properties</h2>
<p>You can delete object properties with the delete keyword.</p>
<p id='demo'></p>
<script>
var person = {
  firstname: 'John',
  lastname: 'Doe',
  age: 50,
  eyecolor: 'blue'
};
delete person.age;
document.getElementById('demo').innerHTML =
person.firstname + ' is ' + person.age + ' years old.';
</script>
</body>
</html>