JavaScript delete Operator
The delete
operator in JavaScript removes a property from an object. It can delete both own properties and inherited properties. Using delete
on an array item leaves a hole.
Syntax:
delete object
// or
delete object.property
// or
delete object['property']
Parameter: It does not take any parameters.
Return type: This operator returns true if it removes a property. While deleting an object property that doesn’t exist will return a true but it will not affect the object. However, while trying to delete a variable or a function will return a false.
JavaScript delete Operator Examples
Example 1: In this example, the salary
property exists in the emp
object, the delete
operation is successful, and true
is logged to the console.
javascript
let emp = {
firstName: "Raj" ,
lastName: "Kumar" ,
salary: 40000
}
console.log( delete emp.salary);
console.log(emp);
|
Output
true
{ firstName: 'Raj', lastName: 'Kumar' }
Explanation:
The code defines an object `emp` with properties `firstName`, `lastName`, and `salary`. `delete emp.salary` attempts to delete the `salary` property. It returns `true` if successful, and `false` otherwise. However, the property is still present in the `emp` object, as demonstrated by the subsequent `console.log(emp)`.
Example 2: Here’s an example illustrating the behavior of delete
with a non-configurable property
javascript
let obj = {
name: "John"
};
Object.defineProperty(obj, 'age' , {
value: 30,
configurable: false
});
console.log(obj);
let result = delete obj.age;
console.log(result);
console.log(obj);
|
Output
{ name: 'John' }
false
{ name: 'John' }
Explanation:
- An object
obj
is defined with properties name
and age
.
- The
age
property is made non-configurable using Object.defineProperty()
with configurable: false
.
- An attempt is made to delete the
age
property using delete obj.age
, which returns false
because the property is non-configurable.
- The
age
property remains unchanged in the object obj
.
Example 3: Here, we are deleting Array Values Using delete
javascript
let arr = [1, 2, 3]
console.log( delete arr[0]);
console.log(arr);
|
Output
true
[ <1 empty item>, 2, 3 ]
Explanation:
delete arr[0]
removes the element at index 0 from the array arr
.
- The deleted element is replaced with an empty slot, represented by the string
'empty'
.
- The array still retains its original length but has an empty slot at index 0.
Example 4: In JavaScript, global properties declared with var
or created without any declaration can be deleted.
Javascript
var globalVar = 10;
let localVar = 20;
console.log( delete globalVar);
console.log( delete localVar);
console.log(globalVar);
console.log(localVar);
|
Explanation:
globalVar
is declared with var
and localVar
with let
.
delete globalVar
returns true
, deleting the global property.
delete localVar
returns false
because let
variables cannot be deleted.
globalVar
becomes undefined
after deletion, but localVar
remains unchanged.
Conclusion:
There are other ways used by developers, such as setting the value of an object property to null or undefined. But the property will still exist on the object and some operators like for in loop will still show the presence of the null or undefined property. Using the delete property in loops slows down the program significantly. So, this method should only be used when it is absolutely necessary to delete an object property.
Last Updated :
07 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...