Recently, I ran some of my JavaScript code through Crockford\'s JSLint, and it gave the following error:
Problem at line 1 character 1: Missing \"use
'use strict';
does not suddenly make your code better.The JavaScript strict mode is a feature in ECMAScript 5. You can enable the strict mode by declaring this in the top of your script/function.
'use strict';
When a JavaScript engine sees this directive, it will start to interpret the code in a special mode. In this mode, errors are thrown up when certain coding practices that could end up being potential bugs are detected (which is the reasoning behind the strict mode).
Consider this example:
var a = 365;
var b = 030;
In their obsession to line up the numeric literals, the developer has inadvertently initialized variable b
with an octal literal. Non-strict mode will interpret this as a numeric literal with value 24
(in base 10). However, strict mode will throw an error.
For a non-exhaustive list of specialties in strict mode, see this answer.
'use strict';
?In my new JavaScript application: Absolutely! Strict mode can be used as a whistleblower when you are doing something stupid with your code.
In my existing JavaScript code: Probably not! If your existing JavaScript code has statements that are prohibited in strict-mode, the application will simply break. If you want strict mode, you should be prepared to debug and correct your existing code. This is why using 'use strict';
does not suddenly make your code better.
Insert a 'use strict';
statement on top of your script:
// File: myscript.js
'use strict';
var a = 2;
....
Note that everything in the file myscript.js
will be interpreted in strict mode.
Or, insert a 'use strict';
statement on top of your function body:
function doSomething() {
'use strict';
...
}
Everything in the lexical scope of function doSomething
will be interpreted in strict mode. The word lexical scope is important here. For example, if your strict code calls a function of a library that is not strict, only your code is executed in strict mode, and not the called function. See this answer for a better explanation.
I found a nice article describing several things that are prohibited in strict mode (note that this is not an exclusive list):
Scope
Historically, JavaScript has been confused about how functions are scoped. Sometimes they seem to be statically scoped, but some features make them behave like they are dynamically scoped. This is confusing, making programs difficult to read and understand. Misunderstanding causes bugs. It also is a problem for performance. Static scoping would permit variable binding to happen at compile time, but the requirement for dynamic scope means the binding must be deferred to runtime, which comes with a significant performance penalty.
Strict mode requires that all variable binding be done statically. That means that the features that previously required dynamic binding must be eliminated or modified. Specifically, the with statement is eliminated, and the eval function’s ability to tamper with the environment of its caller is severely restricted.
One of the benefits of strict code is that tools like YUI Compressor can do a better job when processing it.
Implied Global Variables
JavaScript has implied global variables. If you do not explicitly declare a variable, a global variable is implicitly declared for you. This makes programming easier for beginners because they can neglect some of their basic housekeeping chores. But it makes the management of larger programs much more difficult and it significantly degrades reliability. So in strict mode, implied global variables are no longer created. You should explicitly declare all of your variables.
Global Leakage
There are a number of situations that could cause
this
to be bound to the global object. For example, if you forget to provide thenew
prefix when calling a constructor function, the constructor'sthis
will be bound unexpectedly to the global object, so instead of initializing a new object, it will instead be silently tampering with global variables. In these situations, strict mode will instead bindthis
toundefined
, which will cause the constructor to throw an exception instead, allowing the error to be detected much sooner.Noisy Failure
JavaScript has always had read-only properties, but you could not create them yourself until ES5’s
Object.createProperty
function exposed that capability. If you attempted to assign a value to a read-only property, it would fail silently. The assignment would not change the property’s value, but your program would proceed as though it had. This is an integrity hazard that can cause programs to go into an inconsistent state. In strict mode, attempting to change a read-only property will throw an exception.Octal
The octal (or base 8) representation of numbers was extremely useful when doing machine-level programming on machines whose word sizes were a multiple of 3. You needed octal when working with the CDC 6600 mainframe, which had a word size of 60 bits. If you could read octal, you could look at a word as 20 digits. Two digits represented the op code, and one digit identified one of 8 registers. During the slow transition from machine codes to high level languages, it was thought to be useful to provide octal forms in programming languages.
In C, an extremely unfortunate representation of octalness was selected: Leading zero. So in C,
0100
means 64, not 100, and08
is an error, not 8. Even more unfortunately, this anachronism has been copied into nearly all modern languages, including JavaScript, where it is only used to create errors. It has no other purpose. So in strict mode, octal forms are no longer allowed.Et cetera
The arguments pseudo array becomes a little bit more array-like in ES5. In strict mode, it loses its
callee
andcaller
properties. This makes it possible to pass yourarguments
to untrusted code without giving up a lot of confidential context. Also, thearguments
property of functions is eliminated.In strict mode, duplicate keys in a function literal will produce a syntax error. A function can’t have two parameters with the same name. A function can’t have a variable with the same name as one of its parameters. A function can’t
delete
its own variables. An attempt todelete
a non-configurable property now throws an exception. Primitive values are not implicitly wrapped.
ECMAScript 5 adds a list of reserved words. If you use them as variables or arguments, strict mode will throw an error. The reserved words are:
implements
,interface
,let
,package
,private
,protected
,public
,static
, andyield