โš™๏ธ JavaScript Engine, Execution & Scope
Estimated reading: 4 minutes 11 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 :

Leave a Reply

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

Share

JavaScript โ€” Strict Mode

Or Copy Link

CONTENTS
Scroll to Top