JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Strict Mode

JavaScript's strict mode, introduced in ECMAScript 5, is a way to opt in to a restricted variant of JavaScript.

Strict mode isn't just a subset: it intentionally has different semantics from normal code.

Strict mode makes it easier to write "secure" JavaScript.

Strict mode changes previously accepted "bad syntax" into real errors.

With strict mode, you can not, for example, use undeclared variables.


Invoking Strict Mode

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

To invoke strict mode for an entire script, put the exact statement "use strict"; before any other statements:

Run code

To invoke strict mode for a function, put the exact statement "use strict"; in the function's body before any other statements:

Run code

Strict mode helps you to write cleaner code, like preventing you from using undeclared variables.

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

Both single-quoted and double-quoted syntax are acceptable ('use strict'; or "use strict";).



General Restrictions in Strict Mode

As you already know, in strict mode, all variables must be declared.

If you assign a value to an identifier that is not a declared variable, a ReferenceError will be thrown:

Run code

Using an object, without declaring it, is not allowed (Objects are variables too):

Run code

In strict mode, if you try to delete a variable syntax error will be thrown:

Run code

Similarly, when you try to delete a function in strict mode you will get an syntax error:

Run code

Duplicating a parameter name is not allowed in strict mode:

Run code

Writing to a read-only property is not allowed in strict mode:

Run code

The with Statement is Not Allowed in strict mode:

Run code

Octal literals are Not Allowed in strict mode:

Run code

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

Run code

The string "eval" cannot be used as an identifier (variable name):

Run code

The string "arguments" cannot be used as an identifier (variable name):

Run code

Paving the way for future ECMAScript versions

Future ECMAScript versions will likely introduce new syntax, and strict mode in ES5 applies some restrictions to ease the transition.

It will be easier to make some changes if the foundations of those changes are prohibited in strict mode.

In strict mode, the following list of identifiers become reserved keywords:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield
Run code