Please look at the code,
foo = 1;
delete foo; // true
Object.getOwnPropertyDescriptor(this,'foo').configurable // true
var bar = 2;
delete bar; // false
Object.getOwnPropertyDescriptor(this,'bar').configurable // false
const fooBar = 3;
Object.getOwnPropertyDescriptor(this,'fooBar').configurable // undefined
delete fooBar; //false
Object.getOwnPropertyDescriptor(this,'noexist').configurable // undefined
delete noexist; // true
Based on MDN the delete operator can work only with properties where their own descriptor configurable is true, so why there is a difference between delete "fooBar" and "noexist" returned value?
Variables declared with
constorletdo not get assigned to the global object, hence yourdoes not show up when you do
Only variables declared with
var(or never declared at all, only assigned to, such as withfoo) get assigned to the global object.deletewill return:window.foo, having not been declared withvar,let, orconst, is a configurable property.window.bar, declared with yourvar bar, is assigned towindowas a non-configurable property.delete fooBarreturnsfalsebecausefooBar, although not actually a property onwindow, is a standalone identifier which cannot be deleted -deletewill result infalsewhenever usingdeletelike that would throw an error in strict mode:But
noexistis not an identifier in your code, so there's no operation to even attempt to perform, so it returnstrue(and no error would be thrown in strict mode).