As we all know that JavaScript is a very lenient language in terms of showing errors, usage of variables, type declaration of variable etc which is a double ended sword. Hence the introduction of the strict mode in ECMAScript 5 which allows you to compile and execute your code in a "strict" operating context hence you will see more exceptions with more information shared with the developers.
It's good because it will force you to write good code. It's a perfection depiction of the quote "Fail fast and fail loudly".
Now before getting to the features of "use strict"
, let's learn how to use it.
How to enable Strict Mode?
To enable strict mode for your javascript code, all you have to do is add the string "use strict"
at the beginning of the code.
To make enable strict mode for the entire code in a single JS file, add "use strict"
as the first line of code
"use strict";
/*
Your javascript code
*/
Also, we can enable strict mode for some specific functions too, which will enable strict mode for only that particular function.
function iamStrict()
{
"use strict";
// your javascript code
}
The syntax is neat and just a string that has to be added, nothing else. Now that we know how to use it, let's learn what it brings along - it's features.
Features of "use strict"
statement
First of all, the features of ECMAScript 3 which are deprecated in ECMAScript 5 will lead to exception. They will not be ignored silently. Apart from this, following are a few ill-coding habits that "use strict"
prohibits:
1. No Implicit Global Variables
One cannot use global variables without declaration. Also any variable declaration using var
is not considered as global variable implicitly. To define a variable as global, you must explicitly specify that,
var foo = "bar" // not a global variable
window.foo = "bar" // global variable
Also, if we try to assign foo="bar";
before defining foo
variable will fail.
2. More Exceptions and Error
All the silent failing assignments or other errors will lead to exceptions.
Nan = 5; // lead to exception
3. delete
doesn't work
You cannot use the delete
keyword to delete properties which have their configurable property set to false. For example, the following will lead to error,
function test(arg)
{
delete arg; // ERROR
}
4. Duplicate Property name in Object Literal
If we try to define a property more than once in an object literal we will get error. For example,
var x = {x1: "1", x1: "2"} // will give error
5. eval
is prohibited
Almost any use of eval
is prohibited, so no assignment, no introduction of new variables using eval
and also you cannot use eval as function name or as argument to any function
obj.eval = ... // ERROR
obj.foo = eval; // ERROR
function eval() { ... } // ERROR
// introducing new variable using eval
eval("var a = false;"); // ERROR
6. No more with
statements
While working with strict mode on, we cannot use with
statements anymore. In fact you will get a syntax error for it.
7. Restriction in Functions
Identical names for arguments is not allowed.
function(foo, foo) { ... } // ERROR
Also accessing arguments.caller
and arguments.callee
functions will also throw an exception.
So these are some of the restrictions that "use strict"
will impose on your code, and many of these are just good practices that the strict mode makes compulsory for every developer to follow.
Word of Caution
"use strict"
is not something that you apply on your code and the code becomes better, NO! Applying "use strict"
to existing code can lead to errors and exceptions. Hence it is always advised to have proper unit test cases in place for large applications to test changes in code including applying strict mode to your code.
But yes, strict mode should be enabled for important code libraries to make sure the code is full proof. Rest, it's your decision.