JavaScript Interview Questions: Solve the following code
Solve this JavaScript Interview Question and test your knowledge.
const person = { name: 'Alice', age: 25 };
delete person.age;
console.log(person.age);
Understanding the JavaScript delete Operator
In JavaScript, objects are key-value pairs where properties (also known as keys) are associated with corresponding values. The delete operator is used to remove properties from objects. Once a property is deleted, trying to access it will result in undefined, as it no longer exists on the object.
Let’s break down the given code and explain the flow in detail.
Code Walkthrough
const person = { name: 'Alice', age: 25 };
delete person.age;
console.log(person.age);
1. Creating the Object
const person = { name: 'Alice', age: 25 };
Here, an object person is created with two properties:
name: set to'Alice'age: set to25
At this point, the object looks like:
{
name: 'Alice',
age: 25
}
2. Using the delete Operator
delete person.age;
The delete operator removes the age property from the person object. After this line of code executes, the person object is modified, and the age property is no longer present.
Now, the object looks like:
{
name: 'Alice'
}
The age property has been successfully deleted from the object.
3. Accessing the Deleted Property
console.log(person.age);
Here, you are attempting to log the value of the age property of the person object. However, since the age property was deleted in the previous step, the person object no longer has an age property.
In JavaScript, when you try to access a property that doesn’t exist on an object, the result is undefined. So, when person.age is accessed after the delete operation, it will return undefined.
Output:
undefined
Key Points about the delete Operator
- Non-Destructive to the Object: The delete operator does not remove the object itself but removes the property from the object. If a property is deleted, the object still exists, but the specific property is gone.
- Return Value of delete: The delete operator returns true if the property is successfully deleted, and false if the property cannot be deleted (for example, if the property is non-configurable or part of an object that’s frozen).
- Accessing Deleted Properties: After a property is deleted from an object, any attempt to access it will return undefined.
- Use Case for delete: You might use delete when you want to remove properties from an object dynamically, often when cleaning up or transforming data.
Why Is undefined Logged?
After the delete person.age; statement, the age property is completely removed from the person object. When you try to access it using person.age, it returns undefined because the property no longer exists. This is a fundamental behavior of JavaScript, where any reference to a deleted property results in undefined.
Conclusion
In summary, the output of the code is undefined because the age property is deleted from the object person, and accessing it afterward results in undefined.
Blog Summary
Topic: The delete Operator in JavaScript
The delete operator in JavaScript allows you to remove properties from an object. After a property is deleted, accessing it results in undefined. In this blog, we break down the example where we create an object, use delete to remove a property, and then attempt to access that property.
What Happens in the Code?
- We define an object with properties
nameandage. - The
ageproperty is deleted. - When we try to log
person.age, it printsundefinedbecause the property no longer exists.
This demonstrates how delete works and how properties that no longer exist on an object return undefined when accessed.
Also Read: JavaScript Practical Interview Questions: What will be the output of the following code ?
Read more on this: https://www.keka.com/javascript-coding-interview-questions-and-answers
