Strict Mode in JavaScript: A Comprehensive Guide

priyanshubelwal

Priyanshu Belwal

Posted on January 4, 2024

Strict Mode in JavaScript: A Comprehensive Guide

Javascript is a widely used programming language, famous for its dynamic nature. As a good developer, we tends to write clean, maintainable and error-free code. There are various tools and features which enhances the language's capabilities and helps developers to write more robuts and reliable JavaScript code. Strict Mode is also a set of rules introduced in ECMAScript 5 (ES5) to help developers for the same.

What is Strict Mode?

Strict Mode is a feature in JavaScript that allows developers to opt into a more restricted and disciplined variant of the language. When enabled, it catches common coding mistakes, prevents the use of certain error-prone features, and promotes writing secure and maintainable code. Strict Mode was introduced to address some of the language's design flaws and provide a safer development environment.

To enable Strict Mode, we simply add the following directive to the top of our JavaScript file or within a function:

"use strict";
Enter fullscreen mode Exit fullscreen mode

Why Strict Mode:

There are some benefits included in Strict Mode, which are following:

1. Error Prevention

Strict Mode helps catch common coding errors by turning them into explicit exceptions. For example, in non-strict mode, assigning a value to an undeclared variable would create a global variable. In Strict Mode, this would throw a ReferenceError, preventing accidental global variable leakage.

// Non-strict mode
undeclaredVariable = 42; // No error, creates a global variable

// Strict mode
"use strict";
undeclaredVariable = 42; // Throws a ReferenceError
Enter fullscreen mode Exit fullscreen mode

2. Elimination of Silent Failures

In non-strict mode, some actions may fail silently, leading to unexpected behavior. Strict Mode makes these failures more noticeable by turning them into errors. For instance, assignments to read-only properties and assignments to undeclared variables are flagged as errors in Strict Mode.

// Non-strict mode
var obj = {};
Object.defineProperty(obj, 'x', { value: 42, writable: false });
obj.x = 10; // No error, fails silently

// Strict mode
"use strict";
var obj = {};
Object.defineProperty(obj, 'x', { value: 42, writable: false });
obj.x = 10; // Throws a TypeError
Enter fullscreen mode Exit fullscreen mode

3. Safer Code

Strict Mode prohibits certain "unsafe" actions, such as using with statements and assigning values to read-only properties. By disallowing these practices, developers are encouraged to adopt safer alternatives, leading to more reliable code.

// Non-strict mode
with (Math) {
  result = sqrt(9); // May lead to unexpected behavior
}

// Strict mode
"use strict";
with (Math) {
  result = sqrt(9); // Throws a SyntaxError
}
Enter fullscreen mode Exit fullscreen mode

Strict Mode Restrictions

Below are some of the restrictions in Strict Mode, described in brief:

1. Variable Declarations:
  • Undeclared Variables: Assigning a value to an undeclared variable throws a ReferenceError.
  • Duplicate Parameter Names: Defining a function with duplicate parameter names results in a SyntaxError.
2. Assignments
  • Read-only Properties: Assigning a value to a read-only property or a non-writable global variable throws a TypeError.
  • Assignment to eval and arguments: Assigning values to eval and arguments is prohibited.
3. Functions
  • Octal Syntax: Octal literals (e.g., 0123) are not allowed.
  • with Statements: The use of with statements is forbidden.
  • arguments Object: Writing to the arguments object doesn't affect named parameters.
4. Miscellaneous
  • delete Operator: Deleting variables, functions, or function arguments is not allowed.
  • Restricted Identifiers: Using certain keywords, such as eval and arguments, as variable or function names is not allowed.
  • this Binding: Assigning values to this in non-constructor functions is prohibited.

Conclusion

Strict Mode in JavaScript is a valuable tool for developers aiming to write safer and more maintainable code. By catching common errors, eliminating silent failures, and imposing stricter rules, it helps prevent bugs and enhances code quality. While it may introduce a learning curve for those accustomed to more lenient JavaScript, the long-term benefits in terms of code reliability and maintainability make it a worthwhile addition to any development workflow.

💖 💪 🙅 🚩
priyanshubelwal
Priyanshu Belwal

Posted on January 4, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related