In JavaScript, the delete Operator’s main purpose is to remove a property from an object. In this article, we are going to learn about delete Operator in JavaScript.
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.
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);
|
Outputtrue
{ firstName: 'Raj', lastName: 'Kumar' }
Example 2: In this example, delete person.phone returns true and logging the person object shows that the phone property doesn’t exist anymore.
javascript
let person = {
firstName: "John",
lastName: "Doe",
phone: 12345
}
console.log(delete person.phone);
console.log(person);
|
Outputtrue
{ firstName: 'John', lastName: 'Doe' }
Example 3: In this example, delete operator doesn’t work for variables or function, it returns false and the actual variables and functions remain untouched.
javascript
let num = 5;
let sum = (a, b) => {
return a + b;
}
console.log(delete num);
console.log(delete sum);
|
Example 4: In this example, Without using the var, let or const keyword sets the variable as a global variable and it’ll work as an object property.
javascript
toDelete = 5;
console.log(delete toDelete);
console.log(toDelete);
|
Output:
true
Uncaught ReferenceError: toDelete is not defined
The delete toDelete returns true and trying to access the variable after deleting it throws a reference error as the variable is not defined anymore.
Example 5: In this example, we are deleting Array Values Using delete
javascript
let arr = [1, 2, 3]
console.log(delete arr[0]);
console.log(arr);
|
Outputtrue
[ <1 empty item>, 2, 3 ]
So, using pop(), shift(), or splice() methods is clearly a better approach to deleting array elements.
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
20 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...