What does “use strict” do in JavaScript?

The "use strict" directive in JavaScript enables strict mode, a stricter variant of JavaScript introduced in ECMAScript 5 (ES5). It enforces better coding practices, catches common errors, and prevents unsafe or deprecated behaviors. Here’s a breakdown of its purpose and effects:

What It Does

  1. Eliminates Silent Errors
  • Throws errors for previously ignored actions like:
    • Assigning to undeclared variables (prevents accidental global variables).
    • Modifying read-only properties (e.g., NaN = 5).
    • Deleting variables, functions, or function parameters (e.g., delete x;).
  1. Disallows Problematic Syntax
  • Blocks duplicate parameter names in functions (e.g., function foo(a, a) {}).
  • Forbids octal literals (e.g., 0123 becomes a syntax error; use 0o123 instead).
  • Restricts reserved keywords (e.g., eval, arguments, implements) from being used as variable names.
  1. Changes this Behavior
  • In non-strict mode, this defaults to the global object (e.g., window in browsers) in functions not bound to an object.
  • In strict mode, this is undefined in such cases, preventing accidental global pollution.
  1. Secures eval
  • Variables declared inside eval no longer leak into the outer scope.
  • Prevents eval from introducing new variables in the current scope.
  1. Miscellaneous Improvements
  • Disallows assigning to arguments (e.g., arguments = [...]).
  • Removes with statements, which are error-prone and hinder optimization.

Reasoning Behind Strict Mode

  1. Safer Code
  • Catches common coding mistakes (e.g., typos in variable names) and throws explicit errors.
  • Prevents accidental global variables and unsafe operations.
  1. Optimization
  • Allows JavaScript engines to optimize code more effectively by enforcing predictable behavior.
  1. Future-Proofing
  • Bans syntax likely to conflict with future ECMAScript versions (e.g., reserved keywords).
  1. Best Practices
  • Encourages declarative coding (e.g., using let/const instead of undeclared variables).

How to Enable Strict Mode

Add "use strict"; to the top of a script or beginning of a function:

// Applies to the entire script
"use strict";
function doSomething() {
  // Strict mode applies here
}

// Applies only to the function
function strictFunction() {
  "use strict";
  // Strict mode here
}

Example: Strict Mode vs. Non-Strict Mode

// Non-strict mode
function riskyCode() {
  undeclaredVar = 10; // Creates a global variable (bad practice)
  console.log(this); // Refers to the global object (e.g., window)
}

// Strict mode
function safeCode() {
  "use strict";
  undeclaredVar = 10; // Throws ReferenceError: undeclaredVar is not defined
  console.log(this); // undefined (if not bound to an object)
}

When to Use It

  • Always: Modern JavaScript projects should default to strict mode.
  • Legacy Code: Gradually adopt strict mode by enabling it per function.

Browser Support

Strict mode is supported in all modern browsers and Node.js. Older browsers (pre-ES5, like IE9 and below) ignore it but are largely obsolete.

By enforcing stricter rules, "use strict" helps you write cleaner, more reliable code and avoid pitfalls that lead to bugs.

Leave a Reply

Your email address will not be published. Required fields are marked *