"use strict";(translation: "use strict") is a directive that enables code execution in strict mode . Without this directive, the program runs in unrestricted mode .
Strict mode was introduced in ECMAScript 5, and older browsers ( IE9 and below ) do not support it. That is, they ignore it and run everything in unrestricted mode.
What is it used for "use strict";?
In strict mode,
some errors may be found earlier,
the most dangerous or useless parts of JavaScript are prohibited or throw a runtime error.
How is it used "use strict";?
To enable strict mode for the entire script, you need to put the "use strict";or directive 'use strict';at the beginning.
"use strict";
// el código aquí se ejecuta en el modo estricto
To enable strict mode in a function, you need to put the directive at the beginning of the function.
// el código aquí se ejecuta en el modo irrestricto
function f() {
"use strict";
// el código aquí se ejecuta en el modo estricto
}
// el código aquí se ejecuta en el modo irrestricto
What is the difference between strict mode and unrestricted mode?
In strict mode,
a value cannot be assigned to an undefined variable (spec §11.13.1 ). In unrestricted mode a global variable is created.
(function() {
"use strict";
x = 5; // ReferenceError: x is not defined
})();
x = 5; // crea la variable global x
You also cannot assign a value to a read-only property. In strict mode an error is thrown, and in unrestricted mode it is silently ignored.
(function() {
"use strict";
window.undefined = 5; // TypeError: Cannot assign to read only
})(); // property 'undefined' of [object Object]
window.undefined = 5; // No hace nada
the instruction cannot be usedwith (spec §12.10 ).
(function() {
"use strict";
with(Object) {} // SyntaxError: Strict mode code may not include a with statement
})();
duplicate properties cannot be defined on an object literal (spec §§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 es igual a {a: 2}
duplicate parameters cannot be defined in a function (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); // regresa 2
changes to the object argumentsdo not change the arguments (spec §§10.6 ).
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(); // regresa 5
argument.callery cannot be usedarguments.callee (spec §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
})();
there are more reserved words for future versions of ECMAScript (spec §7.6.1.2 ).
(function () {
"use strict";
var implements, let, private, public, yield, interface, package, protected, static;
})(); // SyntaxError: Unexpected strict mode reserved word
literals in octal base 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";
})(); // regresa "8?"
The use of use-strict, adds to the browser interpreter how to execute the javascript code, this forces you to need to declare the variables before being able to use them, now the use of them can vary, if you want it to affect globally, you must use it at start of the code, or if you want it to affect a function you could use it after the function
function showData(){
var msj = "welcome";
alert(msj)
}
in this function the browser executes the code without any problem. But if we add use-strict.
function showData(){
"use strict";
msj = "welcome";
alert(msj)
}
the browser will throw an error for not declaring the variable before, in order to use it, for this you must declare the variable before using it.
function showData(){
"use strict";
var msj = "welcome";
alert(msj);
}
This and many criteria are part of the use-strict, for example defining a property twice, duplicate parameters etc, check the reference and find examples
"use strict";
(translation: "use strict") is a directive that enables code execution in strict mode . Without this directive, the program runs in unrestricted mode .Strict mode was introduced in ECMAScript 5, and older browsers ( IE9 and below ) do not support it. That is, they ignore it and run everything in unrestricted mode.
What is it used for
"use strict";
?In strict mode,
How is it used
"use strict";
?To enable strict mode for the entire script, you need to put the
"use strict";
or directive'use strict';
at the beginning.To enable strict mode in a function, you need to put the directive at the beginning of the function.
What is the difference between strict mode and unrestricted mode?
In strict mode,
a value cannot be assigned to an undefined variable (spec §11.13.1 ). In unrestricted mode a global variable is created.
You also cannot assign a value to a read-only property. In strict mode an error is thrown, and in unrestricted mode it is silently ignored.
the instruction cannot be used
with
(spec §12.10 ).duplicate properties cannot be defined on an object literal (spec §§11.1.5 ).
duplicate parameters cannot be defined in a function (specification §§13.1 , §15.3.2 ).
changes to the object
arguments
do not change the arguments (spec §§10.6 ).delete
throws an error if the argument is not a modifiable property of an object (spec §§11.4.1 ).eval
you cannot instantiate variables and functions outside of their own context (spec §10.4.2 ).this
is not converted to an object , and whether or not itthis
is converted to the global objectundefined
null
(spec §§10.4.3 ).they cannot be used
eval
orarguments
as names (specification §11.4.4 , §11.4.5 , §11.13 , §12.2.1 , §12.10 , §12.14.1 , §13.1 ).argument.caller
y cannot be usedarguments.callee
(spec §13.2 ).there are more reserved words for future versions of ECMAScript (spec §7.6.1.2 ).
literals in octal base cannot be used (specification B.1.1 , B.1.2 ).
This answer is a translation of my answer to the same question on the Russian site .
The use of use-strict, adds to the browser interpreter how to execute the javascript code, this forces you to need to declare the variables before being able to use them, now the use of them can vary, if you want it to affect globally, you must use it at start of the code, or if you want it to affect a function you could use it after the function
in this function the browser executes the code without any problem. But if we add use-strict.
the browser will throw an error for not declaring the variable before, in order to use it, for this you must declare the variable before using it.
This and many criteria are part of the use-strict, for example defining a property twice, duplicate parameters etc, check the reference and find examples