Normal variables can’t be deleted with delete operator. delete is only meant for properties on an object. delete has nothing to do with directly freeing memory. Memory management is done indirectly via breaking references. This error only happens in strict mode code. In non-strict code, the operation just returns false.(note that testing this in chrome debug tools return true while in browser or node.js return false)
1 2 3 4 5 6 7
"use strict"; // variable let x = 3.14; delete x; // This will cause an error: Delete of an unqualified identifier in strict mode // function functionx(p1, p2) {}; delete x; // Same as above
Octals
1 2
"use strict"; let x = 010; // This will cause an error
Eval to create variables
1 2 3
"use strict"; eval ("var x = 2"); alert (x); // This will cause an error: Uncaught ReferenceError: x is not defined
With statement
⚠ Warning: Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues. See the "Ambiguity Contra" paragraph in the "Description" section below for details.
obj.x = 3.14; // This will cause an error: Cannot assign to read only property 'x' of object '#<Object>'
Other Features
Local scope
1 2 3 4 5 6 7 8 9
<script> x = 3.14; // This will not cause an error. myFunction(); functionmyFunction() { "use strict"; y = 3.14; // This will cause an error (y is not defined). } </script>
Unspecified this in function
Inside functions, the “this” keyword is no longer the global object if not specified: