JavaScript delete is a built-in operator that removes the property from the object. It removes both the value of the property and the property itself. If no more references exist to the same property, it is eventually released automatically.
The delete operator returns a value, and if it succeeds in deleting the property, it will return true. If it fails to remove the property because it is unwritable, it will return false; if in the strict mode, it will throw an error.
Syntax
delete object.property
// or
delete object['property']
Example 1
let appObj = {a: 21, b: 22, c: 23};
console.log(appObj.a);
delete appObj.a;
console.log(appObj);
Output
You need to keep in mind that the delete operator should not be used on predefined JavaScript object properties. This is because it can crash your application.
Example 2
It might be impossible if you try to delete the whole object. This is because it can only remove the properties from the object, not the entire object.
let appObj = {a: 21, b: 22, c: 23};
console.log(appObj);
delete appObj;
console.log(appObj);
Output
Unlike common belief suggests, a delete operator in Javascript has nothing to do with directly freeing memory. Instead, memory management is done indirectly by breaking references.
The delete operator removes the given property from the object. The successful deletion will return true; otherwise, the false will be returned. However, it is essential to consider the following cases.
- If the property you are trying to delete does not exist in the object, then delete will not have any effect and will return true.
- If the property with the same name exists on the object’s prototype chain, then, after the deletion, an object will use the property from the prototype chain. So, it will go for the up hierarchy.
Let’s see the following example of True or False.
let appObj = {a: 21, b: 22, c: 23};
console.log(appObj);
console.log(delete appObj.d);
Output
You can see that if the object property does not exist and you try to remove that property that does not exist, it will give us the true output.
Benchmark of Delete Operator
Delete is the only true way to remove the object’s properties without leftovers, but it works ~ 100 times slower than its “alternative way,” which is the following.
object[key] = undefined
This alternative is not the correct way to remove the property from an object. But, if you use it with care, you can drastically speed up some algorithms.
When to use undefined and when to use delete operator?
The object may be seen as a set of key-value pairs. I call the ‘value’ a primitive or a reference to another object connected to that ‘key’.
Use the delete operator when you pass a result object to the code you don’t control (or when you are unsure about your team or yourself). It deletes the key from the hashmap.
let obj = {
prop: 1
};
delete obj.prop;
Use the undefined when you care about the performance. It can give an excellent boost to your code.
The key remains in the hashmap, and only the value is replaced with an undefined.
Understand that for in loop will still iterate over that key.
let obj = {
prop: 1
};
obj.prop = undefined;
Using the above method, not all-determining the property’s existence will work as expected.
object.prop === undefined
Will behave equivalently for both methods.
Conclusion
The delete operator in JavaScript is used to remove properties from an object. It returns true if it succeeds and false otherwise.