JavaScript Use Strict
JavaScript Use Strict
com
The purpose of "use strict" is to indicate that the code should be executed in "strict
mode".
With strict mode, you can not, for example, use undeclared variables.
All modern browsers support "use strict" except Internet Explorer 9 and lower:
Directive
The numbers in the table specify the first browser version that fully supports the directive.
You can use strict mode in all your programs. It helps you to write cleaner code, like
preventing you from using undeclared variables.
"use strict" is just a string, so IE 9 will not throw an error even if it does not
understand it.
Declared at the beginning of a script, it has global scope (all code in the script will execute
in strict mode):
Example
"use strict";
x = 3.14; // This will cause an error because x is not
declared
Try it Yourself »
Example
"use strict";
myFunction();
function myFunction() {
y = 3.14; // This will also cause an error because y is not
declared
}
Try it Yourself »
Declared inside a function, it has local scope (only the code inside the function is in strict
mode):
function myFunction() {
"use strict";
y = 3.14; // This will cause an error
}
Try it Yourself »
Compiling a numeric literal (4 + 5;) or a string literal ("John Doe";) in a JavaScript program
has no side effects. It simply compiles to a non existing variable and dies.
So "use strict"; only matters to new compilers that "understand" the meaning of it.
Strict mode changes previously accepted "bad syntax" into real errors.
In normal JavaScript, a developer will not receive any error feedback assigning values to
non-writable properties.
In strict mode, any assignment to a non-writable property, a getter-only property, a non-
existing property, a non-existing variable, or a non-existing object, will throw an error.
"use strict";
x = 3.14; // This will cause an error
Try it Yourself »
"use strict";
x = {p1:10, p2:20}; // This will cause an error
Try it Yourself »
"use strict";
var x = 3.14;
delete x; // This will cause an error
Try it Yourself »
Deleting a function is not allowed.
"use strict";
function x(p1, p2) {};
delete x; // This will cause an error
Try it Yourself »
"use strict";
function x(p1, p1) {}; // This will cause an error
Try it Yourself »
"use strict";
var x = 010; // This will cause an error
Try it Yourself »
"use strict";
var x = "\010"; // This will cause an error
Try it Yourself »
Try it Yourself »
"use strict";
var obj = {get x() {return 0} };
Try it Yourself »
"use strict";
delete Object.prototype; // This will cause an error
Try it Yourself »
"use strict";
var eval = 3.14; // This will cause an error
Try it Yourself »
The word arguments cannot be used as a variable:
"use strict";
var arguments = 3.14; // This will cause an error
Try it Yourself »
"use strict";
with (Math){x = cos(2)}; // This will cause an error
Try it Yourself »
For security reasons, eval() is not allowed to create variables in the scope from which
it was called:
"use strict";
eval ("var x = 2");
alert (x); // This will cause an error
Try it Yourself »
The this keyword refers to the object that called the function.
If the object is not specified, functions in strict mode will return undefined and
functions in normal mode will return the global object (window):
"use strict";
function myFunction() {
alert(this); // will alert "undefined"
}
myFunction();
Try it Yourself »
Future Proof!
Keywords reserved for future JavaScript versions can NOT be used as variable names in
strict mode.
These are:
implements
interface
let
package
private
protected
public
static
yield
"use strict";
var public = 1500; // This will cause an error
Try it Yourself »
Watch Out!
The "use strict" directive is only recognized at the beginning of a script or a function.
❮ Previous Next ❯
COLOR PICKER
HOW TO
Tabs
Dropdowns
Accordions
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List
SHARE
CERTIFICATES
HTML
CSS
JavaScript
SQL
Python
PHP
jQuery
Bootstrap
XML
Read More »
REPORT ERROR
PRINT PAGE
FORUM
ABOUT
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
jQuery Tutorial
Java Tutorial
C++ Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
jQuery Reference
Java Reference
Angular Reference
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
jQuery Examples
Java Examples
XML Examples
Web Certificates
HTML Certificate
CSS Certificate
JavaScript Certificate
SQL Certificate
Python Certificate
jQuery Certificate
PHP Certificate
Bootstrap Certificate
XML Certificate
Get Certified »
W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and
basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of
use, cookie and privacy policy. Copyright 1999-2020 by Refsnes Data. All Rights Reserved.
Powered by W3.CSS.