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
I would like to offer a somewhat more founded answer complementing the other answers. I was hoping to edit the most popular answer, but failed. I tried to make it as comprehensive and complete as I could.
You can refer to the MDN documentation for more information.
"use strict"
a directive introduced in ECMAScript 5.
Directives are similar to statements, yet different.
use strict
does not contain key words: The directive is a simple expression statement, which consists of a special string literal (in single or double quotes). JavaScript engines, that do not implement ECMAScript 5, merely see an expression statement without side effects. It is expected that future versions of ECMAScript standards introduce use
as a real key word; the quotes would thereby become obsolete.use strict
can be used only at the beginning of a script or of a function, i.e. it must precede every other (real) statement. It does not have to be the first instruction in a script of function: it can be preceded by other statement expressions that consist of string literals ( and JavaScript implementations can treat them as implementation specific directives). String literals statements, which follow a first real statement (in a script or function) are simple expression statements. Interpreters must not interpret them as directives and they have no effect.The use strict
directive indicates that the following code (in a script or a function) is strict code.
The code in the highest level of a script (code that is not in a function) is considered strict code when the script contains a use strict
directive.
The content of a function is considered strict code when the function itself is defined in a strict code or when the function contains a use strict
directive.
Code that is passed to an eval()
method is considered strict code when eval()
was called from a strict code or contains the use strict
directive itself.
The strict mode of ECMAScript 5 is a restricted subset of the JavaScript language, which eliminates relevant deficits of the language and features more stringent error checking and higher security. The following lists the differences between strict mode and normal mode (of which the first three are particularly important):
with
-statement in strict mode.Object
, then you will get a ReferenceError
. In normal mode the identifier is implicitly declared as a global variable (as a property of the global Object
)this
has the value undefined
in functions that were invoked as functions (not as methods). (In normal mode this
always points to the global Object
). This difference can be used to test if an implementation supports the strict mode:var hasStrictMode = (function() { "use strict"; return this===undefined }());
Also when a function is invoked with call()
or apply
in strict mode, then this
is exactly the value of the first argument of the call()
or apply()
invocation. (In normal mode null
and undefined
are replaced by the global Object
and values, which are not objects, are cast into objects.)
In strict mode you will get a TypeError
, when you try to assign to readonly properties or to define new properties for a non extensible object. (In normal mode both simply fail without error message.)
eval()
, you cannot declare or define variables or functions in the scope of the caller (as you can do it in normal mode). Instead, a new scope is created for eval()
and the variables and functions are within that scope. That scope is destroyed after eval()
finishes execution.SyntaxError
when the delete
operator is followed by a non qualified identifier (a variable, function or function parameter). In normal mode the delete
expression would do nothing and is evaluated to false
.TypeError
when you try to delete a non configurable property. (In normal mode the attempt simply fails and the delete
expression is evaluated to false
).0x
. (In normal mode some implementations do allow octal literals.)eval
and arguments
are treated like keywords. You cannot change their value, cannot assign a value to them, and you cannot use them as names for variables, functions, function parameters or identifiers of a catch block.arguments.caller
and arguments.callee
cause a TypeError
in a function in strict mode. Furthermore, some caller- and arguments properties of functions in strict mode cause a TypeError
when you try to read them.