What does "use strict" do in JavaScript, and what is the reasoning behind it?
Solution 1
Update for ES6 modules
Inside native ECMAScript modules (with import
and export
statements) and ES6 classes, strict mode is always enabled and cannot be disabled.
Original answer
This article about Javascript Strict Mode might interest you: John Resig - ECMAScript 5 Strict Mode, JSON, and More
To quote some interesting parts:
Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.
And:
Strict mode helps out in a couple ways:
- It catches some common coding bloopers, throwing exceptions.
- It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
- It disables features that are confusing or poorly thought out.
Also note you can apply "strict mode" to the whole file... Or you can use it only for a specific function (still quoting from John Resig's article):
// Non-strict code...
(function(){ “use strict”;
// Define your library strictly… })();
// Non-strict code…
Which might be helpful if you have to mix old and new code ;-)
So, I suppose it's a bit like the "use strict"
you can use in Perl (hence the name?): it helps you make fewer errors, by detecting more things that could lead to breakages.
Strict mode is now supported by all major browsers.
Solution 2
It's a new feature of ECMAScript 5. John Resig wrote up a nice summary of it.
It's just a string you put in your JavaScript files (either at the top of your file or inside of a function) that looks like this:
"use strict";
Putting it in your code now shouldn't cause any problems with current browsers as it's just a string. It may cause problems with your code in the future if your code violates the pragma. For instance, if you currently have foo = "bar"
without defining foo
first, your code will start failing...which is a good thing in my opinion.
Solution 3
The statement "use strict";
instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.
List of features (non-exhaustive)
Disallows global variables. (Catches missing
var
declarations and typos in variable names)Silent failing assignments will throw error in strict mode (assigning
NaN = 5;
)Attempts to delete undeletable properties will throw (
delete Object.prototype
)Requires all property names in an object literal to be unique (
var x = {x1: "1", x1: "2"}
)Function parameter names must be unique (
function sum (x, x) {...}
)Forbids octal syntax (
var x = 023;
some devs assume wrongly that a preceding zero does nothing to change the number.)Forbids the
with
keywordeval
in strict mode does not introduce new variablesForbids deleting plain names (
delete x;
)Forbids binding or assignment of the names
eval
andarguments
in any formStrict mode does not alias properties of the
arguments
object with the formal parameters. (e.g. infunction sum (a,b) { return arguments[0] + b;}
This works becausearguments[0]
is bound toa
and so on. ) (Seeexamples
section below to understand the difference)arguments.callee
is not supported
[Ref: Strict mode, Mozilla Developer Network]
Examples:
- Strict mode code doesn't alias properties of arguments objects created within it
function show( msg ){ msg = 42; console.log( msg ); // msg === 42 console.log( arguments[0] ); // arguments === 42 } show( "Hey" );
// In strict mode arguments[i] does not track the value of // the corresponding named argument, nor does a named argument track the value in the corresponding arguments[i] function showStrict( msg ){ “use strict”; msg = 42; console.log( msg ); // msg === 42 console.log( arguments[0] ); // arguments === “Hey” } showStrict( “Hey” );
Solution 4
If people are worried about using use strict
it might be worth checking out this article:
ECMAScript 5 'Strict mode' support in browsers. What does this mean?
NovoGeek.com - Krishna's weblog
It talks about browser support, but more importantly how to deal with it safely:
function isStrictMode(){
return !this;
}
/*
returns false, since 'this' refers to global object and
'!this' becomes false
*/
function isStrictMode(){
"use strict";
return !this;
}
/*
returns true, since in strict mode the keyword 'this'
does not refer to global object, unlike traditional JS.
So here, 'this' is 'undefined' and '!this' becomes true.
*/
Solution 5
A word of caution, all you hard-charging programmers: applying "use strict"
to existing code can be hazardous! This thing is not some feel-good, happy-face sticker that you can slap on the code to make it 'better'. With the "use strict"
pragma, the browser will suddenly THROW exceptions in random places that it never threw before just because at that spot you are doing something that default/loose JavaScript happily allows but strict JavaScript abhors! You may have strictness violations hiding in seldom used calls in your code that will only throw an exception when they do eventually get run - say, in the production environment that your paying customers use!
If you are going to take the plunge, it is a good idea to apply "use strict"
alongside comprehensive unit tests and a strictly configured JSHint build task that will give you some confidence that there is no dark corner of your module that will blow up horribly just because you've turned on Strict Mode. Or, hey, here's another option: just don't add "use strict"
to any of your legacy code, it's probably safer that way, honestly. DEFINITELY DO NOT add "use strict"
to any modules you do not own or maintain, like third party modules.
I think even though it is a deadly caged animal, "use strict"
can be good stuff, but you have to do it right. The best time to go strict is when your project is greenfield and you are starting from scratch. Configure JSHint/JSLint
with all the warnings and options cranked up as tight as your team can stomach, get a good build/test/assert system du jour rigged like Grunt+Karma+Chai
, and only THEN start marking all your new modules as "use strict"
. Be prepared to cure lots of niggly errors and warnings. Make sure everyone understands the gravity by configuring the build to FAIL if JSHint/JSLint
produces any violations.
My project was not a greenfield project when I adopted "use strict"
. As a result, my IDE is full of red marks because I don't have "use strict"
on half my modules, and JSHint complains about that. It's a reminder to me about what refactoring I should do in the future. My goal is to be red mark free due to all of my missing "use strict"
statements, but that is years away now.