I'm fairly new to javascript. I noticed that apparently when operating in "use strict" mode, you can't delete objects. I'm not a huge fan of deleting things (since, in theory, scope should take care of that anyway), but I wonder what was the motivation behind removing this feature?
-
4It's also well explained at: [MDN Strict mode](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/Strict_mode) "*Converting mistakes into errors*" – Gustavo Carvalho May 20 '13 at 15:34
-
1Had the same issue. Found MDN explanation helpful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Delete_in_strict_mode – CyberFonic May 25 '22 at 08:20
3 Answers
The delete
statement is still allowed in strict mode, but some particular uses of it are erroneous. It's only allowed for object properties, not simple names, and only for object properties that can be deleted.
Thus
var a = {x: 0};
delete a.x;
is fine, but
delete Object.prototype;
is not, and neither is
delete a;
(The latter is actually a syntax-level error, while an attempt to delete an undeletable property is a runtime error.)

- 405,095
- 59
- 585
- 614
-
9Interesting. So why prohibit being able to delete the actual var itself? – sircodesalot May 20 '13 at 15:15
-
3@sircodesalot because it's pretty weird to make a local variable just go away. It makes the semantics of a block of code much harder to describe. (I'm not actually sure I've seen an "official" rationale.) – Pointy May 20 '13 at 15:17
-
12@sircodesalot: Variable bindings are not allowed to change within a scope. If a scope contains `var foo`, then all references to `foo` within that scope will refer to a variable that was created for that scope. Otherwise they will refer to a variable `foo` in an outer scope, if one exists. Since variables are not created by executable statements, nor can they be destroyed by them. – supercat Jul 20 '14 at 18:35
-
2window.a seems to work in the case of something global. Is this a useable alternative? – Mark Apr 17 '15 at 15:14
-
`x=1;delete window.x;console.log("x is undefined",typeof x==='undefined')` This works, but you should use scoped variables. Prepend your variables with `var variablename`, and always wrap your code in a function `+function(){....your code here......}()` Then you can set your variables to null or undefined when you are done with them. `+function(){var foo = 1; foo += Math.PI; console.log(foo); foo = undefined;}()` – Tschallacka Aug 30 '16 at 12:35
-
"Syntax-level" error ? when is that ? during the short compilation phase before a page runs ? – doubleOrt Dec 02 '17 at 18:48
-
@Taurus a "syntax error" is one that prevents any of the code in the script block from being executed; it means that the code is syntactically invalid. That's in contrast to a "runtime" error, which is an error that happens *after* the code has been successfully parsed and execution has begun. – Pointy Dec 02 '17 at 21:47
-
-
1Yes, except (as you noted) those happen *almost* at the same time with JavaScript. – Pointy Dec 02 '17 at 21:49
[delete] Explained in detail with example
// The delete statement is still allowed in strict mode, but some particular uses of it are erroneous. It's only allowed for object properties, not simple names, and only for object properties that can be deleted.
// "use strict";
// creates the property adminName on the global scope
adminName = "xyz";
// creates the property empCount on the global scope
// Since we are using var, this is marked as non-configurable. The same is true of let and const.
var empCount = 43;
EmployeeDetails = {
name: "xyz",
age: 5,
designation: "Developer"
};
// adminName is a property of the global scope.
// It can be deleted since it is created without var.
// Therefore, it is configurable.
console.log("delete adminName =", delete adminName); // returns true
// On the contrary, empCount is not configurable,
// since var was used.
console.log("delete empCount =", delete empCount); // returns false
// delete can be used to remove properties from objects
console.log("delete EmployeeDetails.name =", delete EmployeeDetails.name); // returns true
// Even when the property does not exists, it returns "true"
console.log("delete EmployeeDetails.salary =", delete EmployeeDetails.salary); // returns true
// delete does not affect built-in static properties
console.log("delete Math.PI =", delete Math.PI); // returns false
// EmployeeDetails is a property of the global scope.
// Since it defined without "var", it is marked configurable
console.log("delete EmployeeDetails =", delete EmployeeDetails); // returns true
x = 1;
var y = 2;
function f() {
var z = 44;
console.log("delete x =", delete x); // returns true
console.log("delete y =", delete y); // returns false
// delete doesn't affect local variable names
console.log("delete z =", delete z); // returns false
}
f.call();

- 7,771
- 5
- 39
- 55
-
1"// Since we are using var, this is marked as non-configurable. The same is true of let and const." Is not accurate. let and const do not add properties to window. – senocular Jul 09 '18 at 22:06
Reference: Delete_in_strict_mode
Normal variables in JavaScript can't be deleted using the delete operator. The delete operator can only delete properties on an object.
Unlike what common belief suggests, the delete operator has nothing to do with directly freeing memory. Memory management is done indirectly via breaking references, see the memory management page and the delete operator page for more details.
This error only happens in strict mode code. In non-strict code, the operation just returns false.

- 10,165
- 18
- 66
- 97