Skip to content

sureshalagarsamy/javascript-delete-operator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

How delete operator works?

The delete operator is used to delete the property of an object.

The example for delete operator is given below.

var abc = { x: 10 };
var result = (function () {
    delete abc.x;
    return abc.x;
})();

alert(result);

Lets take a minute and guess the output !!

Yes you are correct, It will return undefined.

Ok lets try the same delete with another example

var abc = 10;
var result = (function () {
    delete abc;
    return abc;
})();

alert(result);

Now guess what will be the output?

If you say 'undefined' no you are wrong. The output is 10. But why ????

As i mentioned earlier it is used the delete the object property only. delete operators don’t affect local variables.

Ok lets try the same delete with another example

var Employee = {
  company: 'xyz'
}
var emp1 = Object.create(Employee);
delete emp1.company
console.log(emp1.company);

The output would be xyz. Here, emp1 object has company as it is prototype property. The delete operator doesn’t delete prototype property.

emp1 object doesn’t have company as its own property.

Please find the output for the below object.

console.log(emp1);
console.log(Employee);

image

However, we can delete the company property directly from the Employee object using delete Employee.company.