`strict mode` in JavaScript

`strict mode` in JavaScript

This article explains strict mode in JavaScript.

YouTube Video

strict mode in JavaScript

In JavaScript, strict mode is a feature introduced in ECMAScript 5 (ES5) to make code safer and prevent errors. By using strict mode, certain lenient syntax and potentially risky features are disabled, and stricter error checks are enforced. This allows developers to avoid unexpected behavior and write more robust and high-performance code.

Enabling strict mode

To enable strict mode, add the following directive at the beginning of your code.

1"use strict";

This declaration can be applied to the entire script or on a per-function basis. For example, if applied to the entire script, you can write it as follows:.

1"use strict";
2
3function example() {
4    // Strict mode is applied
5}

If applied per function, you can write it as follows:.

1function example() {
2    "use strict";
3    // Strict mode is applied only within this function
4}

strict mode applies on a per-scope basis, so it can also be enabled within a function. Also, since strict mode is enabled by default in modules, you do not need to declare it individually within the module.

Key features and differences of strict mode

Using strict mode changes some of JavaScript's default behaviors. Here are the main examples.

Prohibition of implicit global variables

In regular JavaScript, omitting variable declarations automatically defines them as global variables, but in strict mode, this results in an error.

1"use strict";
2
3x = 10;  // Error: x is not defined

Variables must always be declared using var, let, or const.

Restriction on the use of reserved words

Potential future JavaScript keywords (e.g., implements, interface, package, etc.) cannot be used as variable or function names in strict mode.

1"use strict";
2
3var package = "strict mode";  // Error: Unexpected strict mode reserved word

Changes in the behavior of this

In normal mode, this inside a function refers to the global object (e.g., window in browsers), but in strict mode, this is undefined.

1"use strict";
2
3function example() {
4    console.log(this);  // undefined
5}
6
7example();

This helps prevent bugs caused by the incorrect use of this.

Prohibition of deleting non-deletable properties

In normal mode, attempting to delete non-deletable object properties is ignored without error, but in strict mode, it throws an error.

1"use strict";
2
3var obj = Object.freeze({name: "John"});
4delete obj.name;  // Error: Cannot delete property 'name'

Prohibition of duplicate parameter names

In normal mode, declaring parameters with the same name multiple times is allowed, but in strict mode, it causes an error.

1"use strict";
2
3function example(a, a) {
4    // Error: Duplicate parameter name not allowed in this context
5}

Prohibition of with statement

The with statement can decrease code readability and is disallowed in strict mode.

1"use strict";
2
3with (Math) {
4    // Error: Strict mode code may not include a with statement
5}

Advantages of strict mode

strict mode improves code safety and efficiency in several ways.

Improved error detection

In strict mode, errors that are overlooked in normal mode are detected, allowing developers to catch potential bugs early. For example, it prevents the creation of unintended global variables and the misuse of reserved words.

Improved performance

In strict mode, JavaScript engines can optimize code more efficiently. As a result, execution speed may improve.

Enhanced code security

The handling of this becomes more strict, preventing access to unnecessary global objects, thereby improving code security. Additionally, the restriction on the use of reserved words avoids future conflicts with keywords.

Compatibility and considerations

strict mode was introduced in ECMAScript 5 and is not supported in browsers released before that. However, enabling strict mode is simply ignored by unsupported browsers, without breaking the script. Therefore, it can usually be used safely.

When introducing strict mode to a large existing project, thorough testing is crucial to avoid unexpected errors.

Conclusion

JavaScript's strict mode is an extremely useful feature for improving code quality. In particular, for large-scale projects or team development, it is recommended to actively use strict mode to prevent unexpected bugs and errors, and to enhance code readability and maintainability.

Understanding and properly applying strict mode helps avoid JavaScript pitfalls and enables safer, more efficient development.

You can follow along with the above article using Visual Studio Code on our YouTube channel. Please also check out the YouTube channel.

YouTube Video