How to Unset a JavaScript Variable
Read this JavaScript tutorial to know why you cannot delete the property that is created with var. Also, know in which cases you shouldn’t use the operator.
To unset a JavaScript variable, you cannot use the <kbd class="highlighted">delete</kbd> operator as it just removes a property from an object and cannot remove a variable. Everything depends on how the global variable or property is defined.
If a variable is created with let, the delete operator cannot unset it:
Variable created with let and the delete operator
If a variable is created without let, var, or const, it becomes an implicit global property. In non-strict mode, the operator can delete it. (Note: In strict mode, deleting an undeclared or implicitly global variable throws a SyntaxError.)
Implicit global variable and the delete operator
Properties created with var cannot be deleted from the global or function scope. Similarly, properties created with let and const cannot be deleted from the scope they were defined within. These declarations create non-configurable properties that the delete operator cannot remove.
The delete Operator
The delete operator is primarily used to remove a property from an object. If no more references are held to the same property, it is released automatically. The operator returns true for all cases except when the property is an own non-configurable property. In such cases, false is returned in non-strict mode.