May I know why it prompts 1 rather than undefined for the following javascript code?
function a(){
var y=1;
delete y;
alert(y);
}
a();
May I know why it prompts 1 rather than undefined for the following javascript code?
function a(){
var y=1;
delete y;
alert(y);
}
a();
delete
operates on the property of an object; not on a non-object.
So, this would work:
var y = { val: 1};
delete y.val;
console.dir(y);
outputs:
{}
From the MDN reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
delete is only effective on an object's properties. It has no effect on variable or function names.