🟨 JavaScript Q31 / 121

What is strict mode?

AI-Powered Answer ✓ Answered

JavaScript's 'strict mode' is a way to opt-in to a restricted variant of JavaScript, intended to catch common coding errors and 'unsafe' actions, improve security, and lead to more robust code. It was introduced in ECMAScript 5 (ES5).

What is Strict Mode?

Strict mode is a feature introduced in ECMAScript 5 (ES5) that allows you to write JavaScript code in a more rigorous and less error-prone way. By enabling strict mode, certain 'bad parts' of JavaScript are eliminated, common coding mistakes throw errors instead of failing silently, and some performance optimizations become possible.

How to Enable Strict Mode

Strict mode can be applied to an entire script or to individual functions. It's enabled by including the string 'use strict'; at the beginning of a script or a function.

Global Scope

javascript
'use strict';

// Code here is executed in strict mode
myVariable = 10; // This will throw an error (ReferenceError) because 'myVariable' is not declared.
console.log(myVariable);

Function Scope

javascript
function myFunction() {
  'use strict';
  // Code here is executed in strict mode
  myOtherVariable = 20; // This will throw an error
  console.log(myOtherVariable);
}

// Code outside this function is not in strict mode
// In non-strict mode, this would create a global variable 'myVariable'
myVariable = 30; 
console.log(myVariable);

Key Changes and Restrictions

Strict mode modifies the behavior of some existing features and introduces new restrictions to make JavaScript more predictable and secure:

  • Eliminates the 'with' statement.
  • Implicit global variables are not allowed (variables must be declared with var, let, or const).
  • Assignments to non-writable properties, getters-only properties, or non-existent properties of a non-extensible object throw errors.
  • Deleting non-configurable properties throws an error.
  • Duplicate parameter names for functions are forbidden.
  • Octal literal syntax is forbidden (e.g., 010 for 8).
  • The this value inside functions called without a specified context (i.e., simple function calls) is undefined instead of the global object (window in browsers, global in Node.js).
  • arguments.caller and arguments.callee are not allowed.
  • Keywords like implements, interface, let, package, private, protected, public, static, and yield cannot be used as variable or function names.

Benefits of Strict Mode

  • Easier Debugging: It turns silently ignored errors into thrown errors, making it easier to discover and fix bugs.
  • Prevents Accidental Globals: Helps avoid polluting the global namespace by requiring explicit variable declarations.
  • Eliminates 'Bad Parts': Removes some of JavaScript's problematic features, leading to more secure and robust code.
  • Enables Optimizations: Code written in strict mode can sometimes be optimized better by JavaScript engines.
  • Future Compatibility: Aligns with future versions of JavaScript that might be even stricter by default.

Given its benefits, it is generally recommended to use strict mode for all new JavaScript code, especially in modules and modern development environments where it is often implicitly enabled or transpiled.