Skip to content Skip to sidebar Skip to footer

Delete Operator Confusion

So I found this technique as a workaround for a global .remove() method in all browsers. a.outerHTML =''; delete a a = your element. My issue.. this is very confusing to me bec

Solution 1:

The delete operator is for deleting properties from an object.

var obj = {x: true};
console.log(obj)    // {x: true}delete obj.x;
console.log(obj);   // {}

I'd strongly recommend this reference: Understanding delete


In your example:

a.outerHTML =''; 
delete a;

The delete operator has nothing to do with the effect of a.outerHTML = ''; That outerHTML line of code is acting on its own to replace portions of the DOM with some new HTML. Remove the delete a and the first line of code will still have the effect on the DOM you see.


In fact, in ES5 strict mode:

delete a;

will throw a SyntaxError. That's how unintended that behavior is.

Post a Comment for "Delete Operator Confusion"