Skip to main content

Use strict in JavaScript

"Use Strict" is a feature in JavaScript that enables strict mode. When strict mode is enabled, certain actions that would otherwise be silently ignored or produce different behavior are instead flagged as errors, which can help to identify and prevent potential bugs in the code.

To enable strict mode in JavaScript, simply add the following statement at the beginning of your script or function:

"use strict";

When this statement is present, the code that follows will be executed in strict mode.

Some of the key differences between strict mode and non-strict mode in JavaScript include:

  • In strict mode, attempting to assign a value to a read-only property or variable (e.g. a variable declared with const or an object property with the writable attribute set to false) will throw a type error, whereas in non-strict mode, the assignment would fail silently or produce unexpected behavior.

  • In strict mode, attempting to delete a variable or function (e.g. using the delete operator) will throw a syntax error, whereas in non-strict mode, the deletion would fail silently or produce unexpected behavior.

  • In strict mode, duplicate property names are not allowed in object literals or classes, whereas in non-strict mode, the duplicates would be silently ignored.

  • In strict mode, the with statement is not allowed, whereas in non-strict mode, it can be used to create unintended side effects and potential bugs.

Enabling strict mode in JavaScript can help to catch potential bugs early and encourage more robust and secure coding practices.

Why Strict Mode?

Strict mode was introduced in ECMAScript 5 (ES5) as a way to address some of the issues and quirks of the JavaScript language, and to make the language more secure and reliable. The main reasons why strict mode was introduced are:

  • Enhanced security: Strict mode disallows certain actions that are potentially dangerous or vulnerable to security exploits.

For example:

The use of eval() and with statements are prohibited in strict mode, which can help prevent malicious code injection and other types of attacks.

  • Better error handling: Strict mode catches certain types of errors that would otherwise be ignored or result in unexpected behavior.

For example:

Attempting to delete a variable or function will throw a syntax error in strict mode, which can help catch programming mistakes early and prevent bugs.

  • Cleaner code: Strict mode disallows certain practices that are considered bad coding habits or may lead to potential issues.

For example:

Strict mode prohibits the use of undeclared variables, which can help prevent variable scope issues and make code more self-contained and modular.

  • Future-proofing: Strict mode adheres to a stricter set of rules and constraints, which makes the code more compliant with future versions of the JavaScript language. As new features and changes are introduced to the language, strict mode ensures that the code will continue to function as expected and not be affected by any new features or changes.

Strict Mode Example

Here's an example of how to use strict mode in JavaScript:

"use strict";

// Code in strict mode

function exampleFunction() {
"use strict";

// Code in strict mode within function
}

// Code not in strict mode

In this example

  • The 'use strict'; directive is added at the beginning of the script and also within the exampleFunction() function.
  • This will enable strict mode for the entire script and also for the code within the function.