use strict in javascript: A practical guide for developers

Explore strict mode in JavaScript with practical examples and best practices. Learn how 'use strict' catches errors, prevents global leaks, and improves debugging across environments.

JavaScripting
JavaScripting Team
·5 min read
Quick AnswerDefinition

The use strict in javascript directive enables strict mode, which enforces a safer subset of JavaScript. It helps catch common coding mistakes by throwing errors for undeclared variables, assignment to read-only properties, and silent type coercions. Use 'use strict' at the top of a script or function to opt in, improving reliability and debuggability.

What is 'use strict' and why it matters

According to JavaScripting, strict mode in JavaScript helps catch mistakes early and enforces safer scripting practices. It is a directive that, when placed at the top of a script or within a function, tells the engine to apply a restricted subset of the language. In strict mode, you cannot assign to undeclared variables, you cannot delete non-configurable properties, and you cannot use some deprecated features. This leads to more robust code and easier debugging, especially in larger codebases where accidental globals are common. For new code, relying on ES modules delivers strict mode by default, but it's important to understand how the directive behaves in traditional scripts and in mixed environments. The examples below illustrate typical outcomes when strict mode is active, including how it changes error reporting and the set of allowed operations.

JavaScript
"use strict"; function test() { undeclaredVar = 1; // ReferenceError } try { test(); } catch (e) { console.log(e.name + ": " + e.message); }
JavaScript
"use strict"; var obj = {}; Object.defineProperty(obj, 'x', { value: 1, writable: false }); try { obj.x = 2; } catch (e) { console.log('Cannot write to read-only property', e.name); }
JavaScript
"use strict"; function f() { return this; } console.log(f()); // undefined (function(){ "use strict"; console.log(this); })();

description_detail_1": null

Steps

Estimated time: 60-90 minutes

  1. 1

    Identify scope and script boundaries

    Audit each JavaScript file to determine where strict mode should apply. Decide if you want global, function-level, or module-level enforcement. In modules, strict mode is implicit, so you can skip explicit directives there.

    Tip: Start with core scripts that historically leak globals or rely on deprecated features.
  2. 2

    Add 'use strict' at the top of files

    Place the directive at the very top of candidate files to enable strict mode for the entire file. This helps catch undeclared variables early and aligns behavior across environments.

    Tip: Place the directive before any variables or function declarations.
  3. 3

    Isolate risky sections with IIFEs

    Wrap non-module scripts in an immediately invoked function expression and declare 'use strict' inside the IIFE to limit scope and reduce risk of global leakage.

    Tip: IIFEs make legacy code safer without large rewrites.
  4. 4

    Run linting and tests

    Enable ESLint rules like no-undef, no-redeclare, and no-implied-eval. Run unit tests and integration tests to surface strict-mode related errors.

    Tip: Automated tests catch 80% of strict-mode regressions.
  5. 5

    Handle browser vs. module differences

    Remember that ES modules already run in strict mode. For older scripts, gradual migration or bundler-based builds can minimize risk.

    Tip: Use module-based architecture to benefit from default strictness.
  6. 6

    Document decisions and edge cases

    Keep a changelog of strict-mode decisions, such as replaced global variable patterns and any syntax that became illegal.

    Tip: Documentation reduces future regressions and developer confusion.
Pro Tip: Prefer ES modules when possible; they enable strict mode by default.
Warning: Avoid using deprecated features and implicit globals; they crash in strict mode.
Note: Test with both Node and browser environments to catch environment-specific issues.
Pro Tip: Use lint rules to enforce undeclared variables and safe property access.

Prerequisites

Required

Optional

  • Familiarity with strict mode concepts (optional deep dive)
    Optional

Keyboard Shortcuts

ActionShortcut
Save filePreserve changes after adding 'use strict'Ctrl+S
FindSearch for 'use strict' occurrencesCtrl+F
Format documentFormat JavaScript to improve readabilityCtrl++F
Go to definitionJump to source of a function used with strict modeF12
Rename symbolRefactor identifiers used under strict modeF2
Toggle integrated terminalRun quick Node.js tests while editingCtrl+`

Questions & Answers

What is the purpose of 'use strict' in JavaScript?

'use strict' activates strict mode, a restricted variant of JavaScript that catches common coding mistakes, prevents silent errors, and disables certain problematic features. It helps improve reliability and debugging, especially in legacy codebases.

Strict mode makes JavaScript safer by catching mistakes early and preventing silent bugs.

Where can I place 'use strict'?

Place 'use strict' at the very top of a script or at the start of a function to enable strict mode for that scope. In ES modules, strict mode is automatic, so the directive is not strictly required.

Put 'use strict' at the top of your file or function; modules already run in strict mode.

What are common strict mode changes I should expect?

Expect errors for undeclared variables, disallowed 'with', restricted 'eval' usage, and disallowing octal literals. 'this' becomes undefined in some function calls, and duplicate parameter names can throw a syntax error.

Strict mode turns some silent issues into errors and changes how certain constructs behave.

Is strict mode still relevant with modern tooling?

Yes. While modules enforce strict mode by default, understanding strict mode helps when working with legacy scripts or non-module code, and assists in teaching good habits.

Strict mode matters for legacy code and when teaching JavaScript basics.

How do I migrate a legacy script to strict mode?

Start by wrapping code in an IIFE and adding 'use strict' inside the function. Then run linting, fix undeclared variables, and gradually enable strict mode in other files. Test thoroughly after each change.

Migrate gradually with IIFEs, linting, and careful testing.

Do modules require 'use strict'?

ES modules run in strict mode by default, so you typically do not need to add 'use strict' to module code. However, understanding the concept remains useful for non-module scripts.

Modules are strict by default, but it's good to know why.

Can I disable strict mode in a script?

Strict mode cannot be turned off once enabled in a scope. If you need relaxed behavior, isolate code in non-strict scopes or avoid enabling strict mode for that section.

You generally can't disable strict mode once it's on within a scope.

What to Remember

  • Enable strict mode to catch common errors early
  • Use at top-of-file or within functions for scope control
  • Understand 'this' binding changes and removed features
  • Leverage modules to achieve automatic strictness
  • Test and lint for smooth migration

Related Articles