What is the purpose of "strict mode" in JavaScript?

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Discover the main rules that will impact your code, as well as the benefits of this interpretation mode!

Article published on 02/02/2021, last updated on 10/08/2026

With the arrival of ECMAScript version 5 (released in 2009) came a feature allowing you to switch from the "classic" interpretation mode of Javascript to a more "strict" mode of the language.

To activate this interpretation mode, simply specify it at the very top of your script, as in the example below:


'use strict';
// Javascript code starting here

Note that "strict-mode" can be activated only at the level of certain functions if needed, to do so simply add the line 'use strict' at the beginning of the code of the function in question.

The "use strict" feature is available in all browsers above Internet Explorer 10.

What advantages?

This feature brings three major advantages:

  • Turning certain silent Javascript errors into visible and blocking errors to improve code maintainability.
  • Allowing the Javascript engine to perform more optimizations at interpretation time, so it is possible that the code will sometimes be faster.
  • Preventing the use of certain keywords that will be used in future versions of the ECMAScript standard.

The introduction of this interpretation mode therefore notably made it possible to prepare the ground for the arrival of new versions of the ECMAScript standard (in this case ES6 for now) by preparing the future syntax as well as preventing certain bad design habits.

What differences?

But in reality, what is the impact of adding this "use strict" directive in your Javascript files?

There are many rules that will change the way your code will be interpreted and executed, but I have selected for you the X most important ones to know:

1 - No more global variables without "var"

As you know, in Javascript it is possible to initialize a variable without it having been declared beforehand, this variable will then be a global variable.

The problem is that during a typo, instead of reassigning a new value to a variable you might accidentally create a new global variable.

If this is the case, strict mode will return you a "ReferenceError" type error.

2 - Real errors during bad variable assignments

If you assign a value to a variable that is "read-only", Javascript will simply do nothing, without showing you an error, but with strict mode, you will trigger an exception, example:


'use strict';
NaN = 5 // TypeError

3 - Error when attempting to delete "protected" properties

Like the previous example, the code below used to execute without triggering an error outside strict mode:


'use strict';
delete Array.prototype // TypeError

4 - Mandatory uniqueness of parameter names

As strange as it may seem, the code below can run without a problem in "classic" mode:


'use strict';
function(data, data){
    return data;
}

But the result will not necessarily be the one expected, surely due to a typo, strict mode therefore requires uniqueness of parameters.

5 - Integers no longer inadvertently transform into octal base

If you have ever preceded a number with an unnecessary zero in Javascript, know that there is a strong chance (if the number contains only digits below 8) that it will be automatically transformed into octal (base 8).

If in your console you simply type 047, you will see that it returns the result 39, for this strict mode prevents this notation and forces the use of the "0o" prefix:


'use strict';
var n = 047; // SyntaxError
var o = 0o47; // Real octal

6 - New reserved keywords

To prepare for the arrival of ES6, strict mode has forbidden the use of several keywords, which are: implements, interface, let, package, private, protected, public, static, and yield.

Note that this rule is not taken into account in browsers that natively support ES6!

For the exhaustive list of changes brought by the "use strict" directive, feel free to check out the specific documentation on MDN Web docs!


Masaaki Komori sur Unsplash

Finished reading this article?
Our newsletter

No spam. Only free content, news, and ever more resources to level up your skills!

Join +1500 developers

Comments (0)

to leave a comment

No comments yet