JavaScript Strict Mode

Strict mode is a way to opt into a restricted variant of JavaScript, introduced in ECMAScript 5 (ES5). It makes it easier to write “secure” JavaScript by catching common coding mistakes and preventing the use of certain unsafe actions.

Enabling Strict Mode

You can enable strict mode in two ways:

  1. Globally: Apply strict mode to the entire script by placing 'use strict'; at the top.
  2. Function Scope: Apply strict mode to a specific function by placing 'use strict'; inside the function.
// Global strict mode
'use strict';
x = 3.14; // Error: x is not defined

// Function-level strict mode
function myFunction() {
  'use strict';
  y = 3.14; // Error: y is not defined
}

Try It Now

Key Features of Strict Mode

  1. Eliminates this coercion:
    • In strict mode, this is undefined in functions that are invoked without a specific context.
      'use strict';
      function test() {
        console.log(this); // undefined
      }
      test();
      

      Try It Now

  2. Prevents accidental globals:
    • Assigning a value to an undeclared variable results in a ReferenceError.
      'use strict';
      myVar = 10; // Error: myVar is not defined
      

      Try It Now

  3. Disallows duplicate parameter names:
    • Function parameters must have unique names.
      'use strict';
      function sum(a, a) { // Error: Duplicate parameter name not allowed
        return a + a;
      }
      

      Try It Now

  4. Throws errors on assignments to read-only properties:
    • Assigning values to non-writable properties throws a TypeError.
      'use strict';
      const obj = {};
      Object.defineProperty(obj, 'x', { value: 42, writable: false });
      obj.x = 9; // Error: Cannot assign to read only property 'x'
      

      Try It Now

  5. Prohibits delete of plain names:
    • delete operations can only be used on properties.
      'use strict';
      var x = 5;
      delete x; // Error: Cannot delete 'x'
      

      Try It Now

  6. Prohibits with statements:
    • The with statement is disallowed in strict mode because it can make code difficult to predict and optimize.
      'use strict';
      with (Math) { // Error: Strict mode code may not include a with statement
        x = random();
      }
      

      Try It Now

  7. Prevents setting eval and arguments as variable names:
    • These are reserved for special use in JavaScript.
      'use strict';
      var eval = 10; // Error: Unexpected eval or arguments in strict mode
      

      Try It Now

  8. Restricts the use of octal literals:
    • Octal literals are not allowed in strict mode.
      'use strict';
      var num = 010; // Error: Octal literals are not allowed in strict mode
      

      Try It Now

Benefits of Strict Mode

  • Enhanced Error Checking: Makes it easier to catch common errors and bugs.
  • Improved Performance: Strict mode can be optimized better by JavaScript engines.
  • Safer Code: Prevents the use of some features that are considered problematic or poorly designed.

 

Use Cases

  • New Projects: It’s a good practice to use strict mode in all new JavaScript projects.
  • Learning Tool: Helps new developers avoid common pitfalls.
  • Security: Makes the codebase more secure by preventing potentially dangerous actions.

Strict mode is a powerful tool to ensure cleaner and more robust JavaScript code, and it’s highly recommended to use it wherever possible.