The "use strict" Directive
The purpose of "use strict" is to indicate that the code should be executed in "strict mode".
With strict mode, you can not, for example, use undeclared variables.
Strict mode is supported in: IE from version 10. Firefox from version 4. Chrome from version 13. Safari from version 5.1. Opera from version 12.
Declaring Strict Mode
Strict mode is declared by adding "use strict"; to the beginning of a script or a function.
Declared at the beginning of a script, it has global scope (all code in the script will execute in strict mode):
Declared inside a function, it has local scope (only the code inside the function is in strict mode):
The "use strict"; Syntax
So "use strict"; only matters to new compilers that "understand" the meaning of it.
Why Strict Mode?
Strict mode changes previously accepted "bad syntax" into real errors.
In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error.
Not Allowed in Strict Mode
Using a variable, without declaring it, is not allowed:
Objects are variables too.
Using an object, without declaring it, is not allowed:
Deleting a variable (or object) is not allowed.
Deleting a function is not allowed.
Duplicating a parameter name is not allowed:
Octal numeric literals are not allowed:
Escape characters are not allowed:
Writing to a read-only property is not allowed:
Writing to a get-only property is not allowed:
Deleting an undeletable property is not allowed:
The string "eval" cannot be used as a variable:
The string "arguments" cannot be used as a variable:
The with statement is not allowed:
For security reasons, eval() is not allowed to create variables in the scope from which it was called:
In function calls like f(), the this value was the global object. In strict mode, it is now undefined.
Future reserved keywords are not allowed in strict mode. These are:
Watch Out! The "use strict" directive is only recognized at the beginning of a script or a function.