JavaScript – Delete Operator

Hey Scripting guys!

I know there are lot of puzzles going on with this delete operator in JavaScript. In this blog, we’ll explore about those puzzles with the help of following cases,

  • It deletes properties.
  • Returns true on success.
  • Returns false on failure.
  • Does not delete property with configurable set to false.
  • Sometimes returns true on failure too.
  • Does not delete variable creating using var, let, const.
  • only delete object own property.
  • Does not delete property from prototype chain.

Deletes Properties

Let’s take the following code as example,

foo = 9;

console.log(delete foo);  //returns true

If we run the above code, we will get the output as true.

Whereas , you have an property defined as var, let, const the property will not be deleted.

var foo = 9;
koo = 10;

console.log(delete foo); //returns false
console.log(delete koo); //returns true

Only deletes object own property

Consider an student object where it has two attributes called name and age,

var student = {
   name: 'foo',
   age: 18
}

console.log(delete student.age); // returns true

So the above delete command will delete the age attribute from student object.

Let’s take a scenario where certain attributes of an object should not be deleted, you can make use configurable property of an attribute.

var student = {
   name: 'foo',
   age: 18
}

Object.defineProperty(Student, 'age',{configurable: false});
console.log(delete student.age) // returns false

If you run the above delete command, it will return false because we have set the configurable property as true.

Sometimes returns true on failure too

Consider the same student object,

var student = {
    name: 'foo',
    age: 18
};

console.log(delete student.address); //returns true

In the above scenario, you can see that student object does not contain address property,

But when you execute above code it will return true. Yea, that’s why I said delete operator is an puzzled one.

Does not delete property from prototype chain

To understand this scenario let’s take two objects animal and dog such as,

var animal = {
   name: 'foo',
   age: 18
}

var dog = {
   name: 'Dog foo',
   isCute: true
}

Dog.__proto__ = animal;
console.log(delete dog.age); // return true

In the above scenario the delete operator will delete the age attribute in animal object because it’s the __proto__ of dog.

console.log(delete dog.name); //return true

In this case, first it will check whether name attribute is present in dog object, if it exists it will delete the attribute.

And if it doesn’t exist it checks in animal object and removes the respective name attribute.

Conclusion

In this blog, we explored about the detailed usage of delete operator in JavaScript. If you have any doubts regarding this operator please mention in the comments.

Let’s discuss and explore together!

Happy Coding! Cheers!

2 thoughts on “JavaScript – Delete Operator

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: