Restricts the use of some potentially-harmful features of JavaScript (e.g., eval, with) and throws syntax errors for certain sloppy code.
Syntax"use strict";
Examples
The following code causes a syntax error because in strict mode all variables must be declared with var
.
"use strict";
function testFunction(){
var testvar = 4;
return testvar;
}
intvar = testFunction();
The following code causes a syntax error because in strict mode all variables must be declared with var
. Even though the strict mode directive (use strict;
) is only added to the function, the whole code breaks because that function does not adhere to the strict mode rules.
function testFunction(){
"use strict";
testvar = 4;
return testvar;
}
var hello = true;
The following code _does not_ cause a syntax error, even though the variable intvar variable is not declared with var
, because the strict mode is scoped to the function only, while the non conformant code is found in a different scope (the global scope).
function testFunction(){
"use strict";
var testvar = 4;
return testvar;
}
intvar = 1;
When the expression "use strict";
is placed at the start of a script or a function body, the code contained in it is parsed under stricter rules than what the default JavaScript language allows. These rules include:
var
).;
).Node.DOCUMENT_FRAGMENT_NODE = 9;
).TODO: Add the complete set of strict mode rules.
AttributionsMicrosoft Developer Network: Windows Internet Explorer JavaScript reference Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4