Author: MDBootstrap
"use strict"; Defines that
JavaScript code should be executed in
"strict mode".
The "use strict" Directive
The "use strict" directive was new in ECMAScript version 5.
It is not a statement, but a literal expression, ignored by earlier versions of JavaScript.
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.
You can use strict mode in all your programs. It helps you to write cleaner code, like preventing you from using undeclared variables.
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):
"use strict";
x = 3.14; // This will cause an error because x is not declared
"use strict";
var x = 3.14; // This will NOT cause an error, because x has been declared
"use strict";
myFunction();
function myFunction() {
y = 3.14; // This will also cause an error because y is not declared
}
Declared inside a function, it has local scope (only the code inside the function is in strict mode):
x = 3.14; // This will not cause an error.
myFunction();
function myFunction() {
"use strict";
y = 3.14; // This will cause an error
}
Why Strict Mode?
Strict mode makes it easier to write "secure" JavaScript.
Strict mode changes previously accepted "bad syntax" into real errors.
As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.
In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.
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
The restrictions of strict mode are much wider. Below are the most important ones.
Using a variable, without declaring it, is not allowed:
"use strict";
x = 3.14; // This will cause an error
Note: Objects and functions are variables too.
Using an object, without declaring it, is not allowed:
"use strict";
x = {p1:10, p2:20}; // This will cause an error
Deleting a variable (or object) is not allowed.
"use strict";
var x = 3.14;
delete x; // This will cause an error
Deleting a function is not allowed.
"use strict";
function x(p1, p2) {};
delete x; // This will cause an error
Duplicating a parameter name is not allowed:
"use strict";
function x(p1, p2) {};
delete x; // This will cause an error
Octal numeric literals are not allowed:
"use strict";
var x = 010; // This will cause an error
Warning: The "use strict" directive is only recognized at the beginning of a script or a function.
Previous lesson Next lesson
Spread the word: