bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/JavaScript Foundations
JavaScript•JavaScript Foundations

JavaScript Use Strict

The "use strict" Directive

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

It defines that JavaScript code should be executed in "strict mode".

It is not a statement. It is 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.

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

"use strict";
x = 3.14; // This will cause an error
because x is not declared

Example

"use strict";
myFunction();
function myFunction() {
  y = 3.14;   // This will also cause an error
  because y is not declared
}

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

Runnable example

x = 3.14;       // This will not cause an error.
myFunction();
function
myFunction() {
  "use strict";
  y = 3.14;   // This will cause an error
}

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:

"use strict";
x = 3.14; // This will cause an error

Objects are variables too.

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

"use strict";
x = {p1:10, p2:20}; // This will cause an error

Deleting a variable (or object) is not allowed.

Runnable example

"use strict";
let x = 3.14;
delete x;                // This
will cause an error

Deleting a function is not allowed.

Runnable example

"use strict";
function x(p1, p2) {};
delete x;
// This will cause an error

Duplicating a parameter name is not allowed:

Runnable example

"use strict";
function x(p1, p1) {};   // This will cause an error

Runnable example

"use strict";
let x = 010;             // This
will cause an error

Runnable example

"use strict";
let x = "\010";            // This will cause an error

Octal escape characters are not allowed

Writing to a read-only property is not allowed:

Runnable example

"use strict";
const obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14;            // This
will cause an error

Writing to a get-only property is not allowed:

Runnable example

"use strict";
const obj = {get x() {return 0} };
obj.x = 3.14;            // This
will cause an error

Deleting an undeletable property is not allowed:

"use strict";
delete Object.prototype; // This will cause an error

The word eval cannot be used as a variable:

Runnable example

"use strict";
let eval = 3.14;         // This will cause an error

The word arguments cannot be used as a variable:

Runnable example

"use strict";
let arguments = 3.14;    // This will cause an error

The with statement is not allowed

"use strict";
with (Math){x = cos(2)}; // This will cause an error

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

"use strict";
eval ("x = 2");
alert (x); // This
will cause an error

Runnable example

"use strict";
eval ("var x = 2");
alert (x);    // This
will cause an error

Runnable example

eval ("let x = 2");
alert (x);        // This
will cause an error

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

Runnable example

"use strict";
function myFunction() {
  alert(this); // will alert "undefined"
}
myFunction();

Watch Out!

The "use strict" directive is only recognized at the beginning of a script or a function.

Previous

JavaScript Function Parameters

Next

JavaScript typeof

This chapter

Overview
51

Lessons

194m

Read time

1. JavaScript Tutorial2. JavaScript Syntax3. JavaScript Operators4. JavaScript if5. JavaScript Loops6. JavaScript Functions7. JavaScript Scope8. JavaScript Data Types9. JavaScript Style Guide10. JavaScript Introduction11. JavaScript Statements12. JavaScript Arithmetic13. JavaScript else14. JavaScript For Loop15. JavaScript Functions16. JavaScript Code Blocks17. JavaScript Primitives18. JavaScript Best Practices19. JavaScript Where To20. JavaScript Comments21. JavaScript Assignment22. The JavaScript Ternary Operator23. JavaScript While Loops24. Invoking JavaScript Functions25. JavaScript Hoisting26. JavaScript Built-In Objects27. JavaScript Common Mistakes28. JavaScript Output29. JavaScript Variables30. JavaScript Comparison31. JavaScript Switch Statement32. JavaScript Break33. JavaScript Function Parameters34. JavaScript Use Strict35. JavaScript typeof36. JavaScript Performance37. JavaScript Let38. JavaScript Conditionals39. JavaScript Booleans40. JavaScript Continue41. JavaScript Function Return42. JavaScript toString()43. JavaScript Const44. JavaScript Logical Operators45. JavaScript Control Flow46. JavaScript Function Arguments47. JavaScript Type Conversion48. JavaScript Datatypes49. JavaScript Function Expressions50. JavaScript Destructuring51. JavaScript Arrow Functions

On this page

The "use strict" DirectiveDeclaring Strict ModeThe "use strict"; SyntaxWhy Strict Mode?Not Allowed in Strict ModeWatch Out!