What is strict mode in JavaScript?

strict mode in JavaScript (JS) was added in the new ECMAScript version 5. strict mode enables certain JS code to behave differently semantically in order to make it more secure. As such, browsers that do not support the use of strict mode execute code written in strict mode differently than the browsers that do.

Syntax

To write JS code in strict mode, use the code below.

'use strict';

strict mode can be applied to the entire script or to individual functions and modules.

Whole script

To apply the use of strict mode to the entire script, simply add the code above at the beginning of the script file, before any other code is written.

'use strict';
// the rest of the code goes here
let first = "first variable";

Individual function

For use in a single function, the line of code is added as the first statement in the function body.

function strictFunc() {
   'use strict';
    var x = 5;
    console.log(x);
}
  • Modules are in strict mode by default.
  • strict mode does not apply to code written between braces ({}).

Use of strict mode

strict mode makes several changes to the way JS code generally behaves. It makes certain mistakes, which are otherwise ignored, throw errors instead. It also performs optimization by fixing mistakes to make code run faster and prevents the use of certain syntax. The details of these changes are provided below.

Example of using strict mode
  1. It is not allowed to create global variables by mistake. All variables and objects must be declared before use.
  2. It is not permissible to delete a variable or function.
  3. Assigning values to variables that would be silently ignored in normal circumstances turns into an error. For instance, assigning a value to NaN would throw an error.
  4. Deleting object properties that are undeletable also produces an error.
  5. It ensures that all parameters of a function are unique.
  6. It does not allow the use of Octal literals with a preceding zero (e.g. 0604), as well as Octal escape literals (e.g. \24).
  7. It prevents eval and argument from being used as variable names.
  8. The eval function is not permitted to create a new variable in the scope of which it is called.
  9. The with keyword cannot be used, making its use a syntax error.
  10. It is not allowed to write to get-only or read-only variables.
  11. Using the this keyword to reference the global document object is not allowed in order to make the script more secure. Instead, it returns undefined.
  12. It reserves certain words for their use as keywords in future JavaScript versions. These words include implements, interface, let, package, private, protected, public, static, and yield.

Code

The effect of using strict mode for some of the cases above is presented in the code below.

'use strict';
num1 = 23; // will throw an error since num1 is not declared
var eval = 10 + 3; // Error since eval can not be used as a variable name.
var undefined = 100; // raises an error since assigning a value to a non-writable variable
console.log(this); // produces 'undefined'.
var octal_num = 067; // gives invalid number error, cannot use octal literals preceding with a 0
delete num1; // cannot delete a local variable in strict mode
Copyright ©2024 Educative, Inc. All rights reserved