Leaderboard
Javascript May 12, 2026
Javascript Delete Operator Object Property

JavaScript Object Deletion — What Happens Here?

This is a daily Javascript challenge from the CodeShot archive. Practice your knowledge of Delete Operator Object Property and improve your technical interview readiness.

const person = { name: "Alice", age: 30 }
delete person.age
console.log(person)
A { name: "Alice", age: undefined }
B { name: "Alice" }
C TypeError: cannot delete
D { name: "Alice", age: null }

Detailed Explanation

Why This Question Matters

At first glance, the delete operator in JavaScript seems like a no-brainer. You want a property gone, you call delete, and it disappears. Simple, right?

But for developers moving from languages like Java or C#, this behavior can be a bit jarring. In those languages, you don't just "delete" a field from a class instance at runtime. JavaScript objects are essentially dynamic maps, which makes them incredibly flexible but also introduces some weird edge cases regarding memory, performance, and the prototype chain.

Understanding exactly what delete does—and what it *doesn't* do—is the difference between writing clean code and spending four hours debugging a "hidden" property that refused to die.

Understanding the Code

Let's look at the snippet:

const person = { name: "Alice", age: 30 }
delete person.age
console.log(person)

Here is the play-by-play of what's happening under the hood:

1. Object Creation: We create an object person. In memory, JavaScript allocates space for this object and maps the keys name and age to their respective values.
2. The Delete Operation: When we run delete person.age, we aren't just setting the value to null or undefined. We are telling the JavaScript engine to remove the property age from the object entirely.
3. The Result: The key age is stripped away. If you were to try and access person.age after this line, it wouldn't return 30 or null; it would return undefined because the property literally no longer exists on that object.

Finding the Correct Answer

If this were a multiple-choice question, the correct answer (Option B) would be { name: "Alice" }.

Why not the others?

Some people guess { name: "Alice", age: undefined }. This is a common mistake. Setting a value to undefined is not the same as deleting the property. If you just set person.age = undefined, the key age still exists in the object; it just happens to hold an empty value. You can prove this by using Object.keys(person), which would still show age in the list. With delete, the key is gone for good.

Others might think the const keyword prevents the deletion. This is another point of confusion. const prevents you from *reassigning* the variable person to a new object (e.g., person = { name: "Bob" } would throw an error). However, const does not make the object itself immutable. You can still add, change, or delete properties inside that object.

Common Mistakes Developers Make

The biggest trap with delete is the Prototype Chain.

Imagine you have a property on a prototype:

const animal = { eats: true };
const dog = Object.create(animal);
dog.barks = true;

delete dog.eats;
console.log(dog.eats); // true!

Wait, why is it still true? Because delete only removes properties from the *instance* itself. It doesn't touch the prototype. When you deleted eats from dog, JavaScript looked at the object, saw it wasn't there, and then looked up the prototype chain to animal, where it found eats: true.

Another "gotcha" is performance. Modern JS engines (like V8) optimize objects by creating "hidden classes." When you use delete, you change the shape of the object, which can kick the object into "dictionary mode." This makes property access slower. In high-performance loops, it's usually better to set a value to null than to use delete.

Real-World Usage

In a production environment, you don't see delete used every day, but it's vital in specific scenarios:

1. Cleaning up State: If you're managing a complex state object in a frontend framework and you need to ensure a specific key is completely removed before sending it to an API (to avoid sending null or undefined keys that the backend might reject), delete is your tool.
2. Dynamic Cache Clearing: When building a custom cache or a registry, you might use delete to remove expired entries to prevent memory leaks.
3. Implementing "Undo" Logic: When removing a specific configuration override from a user settings object to revert it back to the default prototype value.

Key Takeaways

- delete removes the property entirely, not just its value.
- const protects the variable binding, not the contents of the object.
- Deleting a property doesn't affect the prototype chain; it only affects the "own property" of the object.
- For performance-critical code, prefer setting values to null or undefined over using delete to avoid breaking the engine's hidden class optimizations.

Why this matters

Understanding Delete Operator Object Property is crucial for passing technical interviews. In real-world applications, this concept often leads to subtle bugs if not handled correctly. For more details, you can always refer to the official MDN Documentation.

📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.

Ready for your shot?

Join thousands of developers solving one logic puzzle every morning.

Solve Today's Challenge →