Code with the line "use strict";
is not uncommon. For example, libraries often start with this:
(function () {
"use strict";
What does "use strict"
mean and why use it?
In other languages: English • Español
Answer 1, authority 100%
"use strict";
(translation: “use strict”) is a setting that forces code to be processed in strict mode . Without this setting, code is processed in unlimited mode .
Strict mode was introduced in ECMAScript 5, and older browsers (IE9 and earlier ) do not support. That is, they ignore the default setting and everything is processed in unrestricted mode.
Why use "use strict";
?
In strict mode:
- some errors can be found faster
- the more dangerous and not useful JavaScript traits are either prohibited or lead to an error.
How to use "use strict";
?
To enable strict mode in a whole script, put the "use strict";
or 'use strict';
setting at the beginning of the script.
"use strict";
// code is processed here in strict mode
To enable strict mode in a function, put the setting at the beginning of the function code.
// code is processed here in unrestricted mode
function f () {
"use strict";
// code is processed here in strict mode
}
// code is processed here in unrestricted mode
What is the difference between strict mode and unrestricted mode?
In strict mode:
-
cannot be assigned a value to an undefined variable (specification §11.13.1 ). In unrestricted mode, a global variable is created.
(function () { "use strict"; x = 5; // ReferenceError: x is not defined }) (); x = 5; // Creates a global variable x
You also cannot assign a value to a read-only data property.
(function () { "use strict"; window.undefined = 5; // TypeError: Cannot assign to read only }) (); // property 'undefined' of [object Object] window.undefined = 5; // Doing nothing
-
you cannot use the
with
statement (specification §12.10 ).(function () { "use strict"; with (Object) {} // SyntaxError: Strict mode code may not include a with statement }) ();
-
in ES5 you cannot define duplicate properties in an object literal (specification §11.1.5 ).
(function () { "use strict"; var x = { a: 1, a: 2 }; // SyntaxError: Duplicate data property in object literal }) (); // not allowed in strict mode var x = { a: 1, a: 2 }; // x is equal to {a: 2}
-
duplicate formal parameters of a function cannot be defined (specification §13.1 , §15.3.2 ).
function f (a, a) { "use strict"; } // SyntaxError: Strict mode function may not have duplicate parameter names function f (a, a) { return a; } f (1,2); // returns 2
-
changes to the
arguments
object do not change the arguments (specification §10.6 ).function f (x) { "use strict"; arguments [0] = 5; return x; } f (10); // returns 10 function f (x) { arguments [0] = 5; return x; } f (10); // returns 5
-
delete
results in an error if the argument is an unmodifiable property of the object (specification §11.4.1 ).(function f (x) { "use strict"; var y = 4; delete f; // SyntaxError: Delete of an unqualified identifier in strict mode. delete x; // SyntaxError: Delete of an unqualified identifier in strict mode. delete y; // SyntaxError: Delete of an unqualified identifier in strict mode. delete window.undefined; // TypeError: Cannot delete property }) (); // 'undefined' of [object Object] (function f (x) { var y = 4; delete f; // Returns false delete x; // Returns false delete y; // Returns false delete window.undefined; // Returns false }) ();
-
eval
cannot instantiate variables and functions in the context of a call (specification §10.4.2 ).(function () { "use strict"; eval ("var x = 5"); return x; // ReferenceError: x is not defined }) (); (function () { eval ("var x = 5"); return x; }) (); // Returns 5
-
this
is not converted to object , but ifthis
isundefined
ornull
, it is not converted to the global object (specification §10.4.3 ).function f () { "use strict"; return this; }; f.call (4); // returns 4 f.call (null); // returns null f.call (undefined); // returns undefined function f () { return this; }; f.call (4); // returns [object Number] f.call (null); // returns [object global] f.call (undefined); // returns [object global]
-
eval
andarguments
– cannot be changed or used as a name (specification §11.4.4 , §11.4.5 , §11.13 , §12.2.1 , §12.10 , §12.14.1 , §13.1 ) .function eval (arguments) {// SyntaxError: Unexpected eval or arguments in strict mode "use strict"; eval = "5"; // SyntaxError: Unexpected eval or arguments in strict mode ++ eval; // SyntaxError: Unexpected eval or arguments in strict mode arguments ++; // SyntaxError: Unexpected eval or arguments in strict mode try { var arguments = 5; // SyntaxError: Unexpected eval or arguments in strict mode } catch (eval) {} // SyntaxError: Unexpected eval or arguments in strict mode return arguments.eval; } function eval (arguments) { eval = "5"; ++ eval; arguments ++; try { var arguments = 5; } catch (eval) {} return arguments.eval; } eval (); // returns 5
-
argument.caller
andarguments.callee
cannot be used (specification §13.2 ).(function f () { "use strict"; arguments.caller; // TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them arguments.callee; // TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them f.arguments; // TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them }) ();
-
more words reserved for future use (specification §7.6.1.2 ).
(function () { "use strict"; var implements, let, private, public, yield, interface, package, protected, static; }) (); // SyntaxError: Unexpected strict mode reserved word
-
octal CC literals cannot be used (specification B .1.1 , B.1.2 ).
(function () { "use strict"; return 010 + // SyntaxError: Octal literals are not allowed in strict mode. "\ 077"; // SyntaxError: Octal literals are not allowed in strict mode. }) (); (function () { return 010 + "\ 077"; }) (); // returns "8?"
Answer 2
In a nutshell, ‘use strict’ means that you are using ES6 (ECMAScript6), the latest JavaScript standard, which differs in syntax from the previous one (ES5).