The delete operator is one of those little things in JavaScript that doesn't get much attention, but when it does, it leaves developers scratching their heads.
So, what can you actually delete with it?
Does it destroy objects?
Variables?
Entire functions?
Let’s break it down.
What Exactly Is the delete Operator?
The delete operator is used to remove properties from objects. Notice the emphasis on properties—not variables, not functions, and certainly not some random block of memory.
Think of it as your Marie Kondo in JavaScript, except instead of tidying up your whole codebase, it’s just clearing space in your objects.
Example: Deleting Properties from Objects
let person = {
name: 'John Doe',
age: 30,
occupation: 'Developer'
};
delete person.age;
console.log(person);
// Output: { name: 'John Doe', occupation: 'Developer' }
In this example, the age property gets removed, and you're left with name and occupation.
What You Can’t Delete: Variables
A common misconception is thinking that delete can be used to get rid of variables. Let’s clear that up: it cannot.
let myVar = 42;
delete myVar;
console.log(myVar);
// Output: 42
In this case, myVar stays stubbornly in place because delete only works on object properties, not variables defined with let, const, or var.
Deleting Global Variables (But Only Sometimes)
If a variable is part of the global object, delete can sometimes work, depending on how the variable was declared.
var globalVar = 'I’m global!';
delete globalVar;
console.log(globalVar);
// Output: 'I’m global!'
Even though globalVar is a property of the global object, delete won’t touch it because it’s declared with var.
However, if you don’t use var (or let or const), and accidentally create a global variable, delete can remove it:
globalOops = 'Oops, I’m global!';
delete globalOops;
console.log(globalOops);
// Output: ReferenceError: globalOops is not defined
Deleting Array Elements (Spoiler: Don’t Do It)
You might think, “Hey, I can use delete to remove an element from an array.” Technically, you’re right, but it’s a bit of a trap.
let myArray = [1, 2, 3];
delete myArray[1];
console.log(myArray);
// Output: [1, empty, 3]
What you end up with is a hole in the array, which isn’t very useful. Instead of delete, use splice if you want to remove an array element:
myArray.splice(1, 1);
console.log(myArray);
// Output: [1, 3]
Much cleaner, right?
delete and Non-Configurable Properties
The delete operator can’t touch properties that are marked as non-configurable.
const obj = {};
Object.defineProperty(obj, 'locked', {
value: 'You can’t delete me!',
configurable: false
});
delete obj.locked;
console.log(obj.locked);
// Output: 'You can’t delete me!'
No matter how hard we try, delete just can’t touch it because it's non-configurable.
Using delete with Strict Mode
In strict mode ('use strict';), delete behaves differently. If you try to delete something that can’t be deleted (like a non-configurable property), JavaScript will throw an error.
'use strict';
const obj = { prop: 'Hello' };
Object.freeze(obj);
delete obj.prop; // Error: Cannot delete property 'prop' of #<Object>
Recap: What Can You Delete?
- Object properties? Yes, as long as they’re not non-configurable.
- Variables? No (unless they’re accidentally global).
- Array elements? Yes, but don’t — it leaves empty slots.
- Non-configurable properties? Nope, not gonna happen.
Conclusion
The delete operator works great for removing properties from an object, but it’s not meant for chopping out variables or array elements. Understanding its quirks will save you a ton of headaches.
Next time you’re thinking about using delete, ask yourself: "Am I trying to remove an object property, or am I asking delete to do something it wasn’t designed for?"
With the right knowledge, you can make delete work exactly how you want.

0 Comments