Home JavaScript Tutorial The modern mode, “use strict” in JavaScript

The modern mode, “use strict” in JavaScript

by Anup Maurya
10 minutes read

In JavaScript, “use strict” is a modern mode that is used to enable a stricter version of the language. When you enable this mode, the JavaScript engine will enforce more rules and restrictions to prevent common mistakes and make your code more reliable. In this tutorial, we’ll explore what “use strict” is, how to enable it, and what benefits it brings.

What is “use strict”?

“Use strict” is a directive in JavaScript that enables a stricter version of the language. It was introduced in ECMAScript 5 (ES5) as a way to address some of the shortcomings and ambiguities of the original language. When you enable strict mode, the JavaScript engine will enforce a set of rules that were previously optional or undefined. This includes rules like:

  • Variables must be declared before they are used.
  • Functions cannot be declared inside blocks.
  • Duplicate object keys are not allowed in objects.
  • The “this” keyword behaves differently in strict mode.

These rules make your code less error-prone and easier to read and maintain. They also prevent some common mistakes that can lead to security vulnerabilities or performance issues.

How to enable “use strict”

To enable strict mode, you need to add the “use strict” directive at the beginning of your code. This can be done in two ways:

1. Global strict mode

You can enable strict mode for the entire script by adding the “use strict” directive at the beginning of the file:

"use strict";

// your code here

This will enable strict mode for all the code in the file, including functions and nested scopes.

2. Function strict mode

You can also enable strict mode for a specific function by adding the “use strict” directive at the beginning of the function:

function myFunction() {
  "use strict";

  // your code here
}

This will enable strict mode only for the code inside the function. If you have nested functions, you can enable strict mode for them individually.

Benefits of “use strict”

Enabling strict mode brings several benefits to your code, including:

1. Better error handling

Strict mode makes the JavaScript engine more strict in its error handling. It will throw more errors for common mistakes, such as using undeclared variables or assigning values to read-only properties. This helps you catch errors early and fix them before they cause more problems.

2. Improved performance

Strict mode allows the JavaScript engine to optimize your code more aggressively. It can make certain assumptions about your code that would otherwise be impossible, such as assuming that variables are always defined before they are used. This can lead to significant performance improvements in some cases.

3. More readable and maintainable code

Strict mode enforces a set of rules that make your code more consistent and predictable. This makes it easier to read and understand, especially for other developers who may be working on the same codebase. It also reduces the likelihood of bugs and makes your code more maintainable over time.

Conclusion

“Use strict” is a modern mode in JavaScript that enables a stricter version of the language. Enabling strict mode brings several benefits to your code, including better error handling, improved performance, and more readable and maintainable code. To enable strict mode, you need to add the “use strict” directive at the beginning of your code, either globally or for specific functions. By using strict mode, you can write more reliable and efficient JavaScript code.

related posts

Leave a Comment