VARIABLES
How and when variables get created is different in strict mode. The first change disallows accidental creation of global variables. In nonstrict mode, the following creates a global variable:
// Variable is not declared
// Non-strict mode: creates a global
// Strict mode: Throws a ReferenceError
message = "Hello world!";
Even though message
isn't preceded by the let
keyword and isn't explicitly defined as a property of the global object, it is still automatically created as a global. In strict mode, assigning a value to an undeclared variable throws a ReferenceError
when the code is executed.
A related change is the inability to call delete
on a variable. Nonstrict mode allows this and may silently fail (returning false
). In strict mode, an attempt to delete a variable causes an error:
// Deleting a variable
// Non-strict mode: Fails silently
// Strict mode: Throws a ReferenceError
let color = "red";
delete color;
Strict mode also imposes restrictions...