JavaScript Use Strict

By | August 27, 2022

"use strict"; Defines that JavaScript code should be executed in “strict mode”.

The “use strict” Directive

The "use strict" directive was new in ECMAScript version 5.

It is not a statement, but a literal expression, ignored by earlier versions of JavaScript.

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:

Directivechromeedgefirefoxsafariopera
“use strict”13.010.04.06.012.1

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.

Declaring Strict Mode

Strict mode is declared by adding “use strict”; to the beginning of a script or a function.

Declared at the beginning of a script, it has global scope (all code in the script will execute in strict mode):

Example

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Using a variable without declaring it, is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
x = 3.14;  // This will cause an error (x is not defined).
</script>
</body>
</html>

Result:

With “use strict”:

Using a variable without declaring it, is not allowed.

Activate debugging in your browser (F12) to see the error report.

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Global "use strict" declaration.</h2>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
myFunction();
function myFunction() {
  y = 3.14;   // This will cause an error (y is not defined)
}
</script>
</body>
</html>

Result:

Global “use strict” declaration.

Activate debugging in your browser (F12) to see the error report.

Declared inside a function, it has local scope (only the code inside the function is in strict mode):

Example:

<!DOCTYPE html>
<html>
<body>
<p>"use strict" in a function will only cause errors in that function.</p>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
x = 3.14;    // This will not cause an error.
myFunction();
function myFunction() {
  "use strict";
  y = 3.14;  // This will cause an error (y is not defined).
}
</script>
</body>
</html>

Result:

“use strict” in a function will only cause errors in that function.

Activate debugging in your browser (F12) to see the error report.

The “use strict”; Syntax

The syntax, for declaring strict mode, was designed to be compatible with older versions of JavaScript.

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.

Why Strict Mode?

Strict mode makes it easier to write “secure” JavaScript.

Strict mode changes previously accepted “bad syntax” into real errors.

As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.

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.

Not Allowed in Strict Mode

Using a variable, without declaring it, is not allowed:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Using a variable without declaring it, is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
x = 3.14;  // This will cause an error (x is not defined).
</script>
</body>
</html>

Result:

With “use strict”:

Using a variable without declaring it, is not allowed.

Activate debugging in your browser (F12) to see the error report.

Objects are variables too.

Using an object, without declaring it, is not allowed:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Using an object without declaring it, is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
x = {p1:10, p2:20};   // This will cause an error (x is not defined).
</script>
</body>
</html>

Result:

With “use strict”:

Using an object without declaring it, is not allowed.

Activate debugging in your browser (F12) to see the error report.

Deleting a variable (or object) is not allowed.

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Deleting a variable (or object) is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let x = 3.14;
delete x;     // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

Deleting a variable (or object) is not allowed.

Activate debugging in your browser (F12) to see the error report.

Deleting a function is not allowed.

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Deleting a function is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
function x(p1, p2) {}; 
delete x;        // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

Deleting a function is not allowed.

Activate debugging in your browser (F12) to see the error report.

Duplicating a parameter name is not allowed:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Duplicating a parameter name is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
function x(p1, p1) {};   // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

Duplicating a parameter name is not allowed.

Activate debugging in your browser (F12) to see the error report.

Octal numeric literals are not allowed:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Octal numeric literals are not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let x = 010;   // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

Octal numeric literals are not allowed.

Activate debugging in your browser (F12) to see the error report.

Octal escape characters are not allowed:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Octal escape characters are not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let x = "\010";   // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

Octal escape characters are not allowed.

Activate debugging in your browser (F12) to see the error report.

Writing to a read-only property is not allowed:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Writing to a read-only property is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
const obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14;   // This will cause an error
</script>
</body>
</html>

Result:

With “use strict”:

Writing to a read-only property is not allowed.

Activate debugging in your browser (F12) to see the error report.

Writing to a get-only property is not allowed:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Writing to a get-only property is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
const obj = {get x() {return 0} };
​obj.x = 3.14;   // This will cause an error
</script>
</body>
</html>

Result:

With “use strict”:

Writing to a get-only property is not allowed.

Activate debugging in your browser (F12) to see the error report.

Deleting an undeletable property is not allowed:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Deleting an udeletable property is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
delete Object.prototype;   // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

Deleting an udeletable property is not allowed.

Activate debugging in your browser (F12) to see the error report.

The word eval cannot be used as a variable:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>The string "eval" cannot be used as a variable.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let eval = 3.14;   // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

The string “eval” cannot be used as a variable.

Activate debugging in your browser (F12) to see the error report.

The word arguments cannot be used as a variable:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>The string "arguments" cannot be used as a variable.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let arguments = 3.14;   // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

The string “arguments” cannot be used as a variable.

Activate debugging in your browser (F12) to see the error report.

The with statement is not allowed:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>The with statement is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
with (Math){x = cos(2)};   // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

The with statement is not allowed.

Activate debugging in your browser (F12) to see the error report.

For security reasons, eval() is not allowed to create variables in the scope from which it was called.

In strict mode, a variable can not be used before it is declared:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>For security reasons, eval() is not allowed to create variables in the scope from which it was called.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
eval ("x = 2");
alert (x);      // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

For security reasons, eval() is not allowed to create variables in the scope from which it was called.

Activate debugging in your browser (F12) to see the error report.

In strict mode, eval() can not declare a variable using the var keyword:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>For security reasons, eval() is not allowed to create variables in the scope from which it was called.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
eval ("var x = 2");
alert (x);      // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

For security reasons, eval() is not allowed to create variables in the scope from which it was called.

Activate debugging in your browser (F12) to see the error report.

eval() can not declare a variable using the let keyword:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Using eval()</h2>
<h3>For security reasons, eval() is not allowed to create variables in the scope from which it was called.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
eval (“let x = 2”);
alert (x);      // This will cause an error
</script>
</body>
</html>

Result:

Using eval()

For security reasons, eval() is not allowed to create variables in the scope from which it was called.

Activate debugging in your browser (F12) to see the error report.

The this keyword in functions behaves differently in strict mode.

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):

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Inside functions, the "this" keyword is no longer the global object if not specified:</h3>
<script>
"use strict";
function myFunction() {
  alert(this);
}
myFunction();
</script>
</body>
</html>

Result:

With “use strict”:

Inside functions, the “this” keyword is no longer the global object if not specified:

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

Example:

<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Future reserved keywords are not allowed in strict mode.
</h3><p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let public = 1500;   // This will cause an error 
</script>
</body>
</html>

Result:

With “use strict”:

Future reserved keywords are not allowed in strict mode.

Activate debugging in your browser (F12) to see the error report.

Leave a Reply

Your email address will not be published. Required fields are marked *