Using the JavaScript delete operator you can remove a property from an object. The delete operator deletes both the value of the property and the property itself.
delete object
// or
delete object.property
// or
delete object['property']
Note: the delete operator only works on objects and not on variables or functions.
JavaScript delete operator
Simple example code.
<!DOCTYPE html>
<html>
<body>
<script >
const Employee = {
firstname: 'John',
lastname: 'King'
};
console.log(Employee);
delete Employee.firstname;
console.log(Employee);
</script>
</body>
</html>
Output:
Let’s try applying the delete operator for deleting a variable and a function. The delete operator doesn’t work for variables or function
<script >
let num = 5;
let sum = (a, b) => {
return a + b;
}
console.log(delete num); //false
console.log(delete sum); //false
</script>
Do comment if you have any doubts or suggestions on this Js operator topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version