โš™๏ธ JavaScript Engine, Execution & Scope
Estimated reading: 4 minutes 279 views

JavaScript โ€” Strict Mode: Enforcing Safer Coding Practices


Introduction โ€“ Why Strict Mode Matters in JavaScript

JavaScript is a flexible and forgiving languageโ€”but that flexibility can sometimes lead to bugs, insecure code, or hard-to-find mistakes. This is where Strict Mode comes in.

By enabling Strict Mode, you enforce a stricter set of rules in your code, helping prevent accidental errors and enforcing better coding standards.

By the end of this guide, youโ€™ll learn:

  • How to enable Strict Mode
  • What errors it catches
  • Best practices and common pitfalls it prevents
  • Real-world examples for both functions and scripts

What is JavaScript Strict Mode?

Strict Mode is a feature introduced in ES5 (ECMAScript 2009) that helps developers write cleaner, more secure JavaScript code.

๐Ÿšฆ Syntax

"use strict";

This directive tells the browser (or Node.js) to execute the code in Strict Mode.


How to Enable Strict Mode

You can enable it:

Globally in a Script

"use strict";
let x = 5;
console.log(x);

This applies strict mode to the entire script.

Locally in a Function

function myFunction() {
  "use strict";
  let y = 10;
  return y;
}

This applies strict mode only inside myFunction.

Note: Placing "use strict"; at the top is mandatoryโ€”it wonโ€™t work otherwise.


What Errors Does Strict Mode Catch?

Strict Mode helps eliminate silent errors and prevents unsafe actions. Here’s what it restricts:

1๏ธโƒฃ Assigning to Undeclared Variables

"use strict";
x = 10; //  ReferenceError

In non-strict mode, x would become a global variable.


2๏ธโƒฃ Deleting Undeletable Properties

"use strict";
delete Object.prototype; //  TypeError

Prevents removal of built-in properties.


3๏ธโƒฃ Duplicating Parameter Names

"use strict";
function test(a, a) {} //  SyntaxError

Avoids confusion in function scopes.


4๏ธโƒฃ Using Reserved Keywords

"use strict";
let public = 123; //  SyntaxError

Protects future JavaScript compatibility.


5๏ธโƒฃ Writing to Read-only Properties

"use strict";
const obj = {};
Object.defineProperty(obj, "readonly", { value: 42, writable: false });
obj.readonly = 100; //  TypeError

Prevents silent assignment failures.


6๏ธโƒฃ Using with Statement

"use strict";
with (Math) { x = cos(2); } //  SyntaxError

with is disallowed in Strict Mode due to scope unpredictability.


Example: Strict Mode vs Non-Strict Mode

CodeNon-Strict Mode BehaviorStrict Mode Behavior
x = 10;Creates global variable Throws ReferenceError
delete Object.prototype; Fails silently Throws TypeError
function(a, a) {} Allowed SyntaxError
with(obj) {} Allowed SyntaxError

Benefits of Using Strict Mode

  • Helps catch common coding bloopers
  • Prevents usage of unsafe features
  • Improves code readability and maintainability
  • Makes it easier to write “secure” JavaScript
  • Prepares your codebase for future ECMAScript standards

Things to Keep in Mind

Tips

  • Always use "use strict"; at the top of scripts or functions.
  • Avoid using global variables and undeclared assignments.
  • Use ES6 features like let, const, and arrow functions with Strict Mode for even better safety.

Notes

  • Modules and classes are strict by default in JavaScript.
  • "use strict"; has no effect if placed in the middle of code or after other statements.

Real-World Use Case

Letโ€™s compare two similar scripts:

Without Strict Mode

function calculatePrice(price) {
  tax = 0.15; // silently creates global variable
  return price + (price * tax);
}

Risk: Global variable tax might collide elsewhere in your app.


With Strict Mode

function calculatePrice(price) {
  "use strict";
  tax = 0.15; //  ReferenceError
  return price + (price * tax);
}

Fix:

function calculatePrice(price) {
  "use strict";
  let tax = 0.15;
  return price + (price * tax);
}

Summary

Strict Mode enforces safer, cleaner, and more predictable JavaScript coding. By enabling it, you avoid many silent errors and unintentional bugs.

Use "use strict"; at the beginning of your scripts or functions.
Combine it with modern ES6 features for robust, maintainable code.


FAQs โ€“ JavaScript Strict Mode

Is "use strict"; required in modern JavaScript?

No, but itโ€™s highly recommended, especially in scripts. However, ES6 modules and classes are strict by default.

Does "use strict"; work in Node.js?

Yes. Node.js also supports Strict Mode in the same way as browsers.

Can I use Strict Mode in ES6 arrow functions?

Yes, just place "use strict"; at the top of your file or function. Note: modules are strict automatically.

What happens if I forget to use "use strict";?

Your code will still run, but it may allow poor practices like implicit globals, duplicate parameters, and unsafe assignments.

Does Strict Mode impact performance?

Not significantly. In fact, it may allow JavaScript engines to optimize code better because of its predictability.


Share Now :
Share

JavaScript โ€” Strict Mode

Or Copy Link

CONTENTS
Scroll to Top