top of page

JavaScript strict mode
pros and cons

You might have seen 'use strict' in the top of JavaScript files?

It says 'strict mode' so...it's strict then. More strict than normal, whatever that means.

That is the point! What does it actually mean? Does it make our code better? Does it only have benefits? 

 

We will look at both the pros and the cons of using 'strict mode' in JavaScript.  

strict-mode.png

Strict mode in JavaScript is a feature that was introduced in ECMAScript 5 to enable a stricter mode of operation in JavaScript.

In 'strict mode', certain behaviors that were previously allowed in the language are disallowed or changed, which can lead to some pros and cons.

​

Let's look at some pros of JavaScript strict mode:

  1. More secure code: Strict mode helps to reduce the number of potential security vulnerabilities in JavaScript code. It disallows the use of certain features that can be exploited by attackers, such as the with statement and the use of undeclared variables.

  2. Better error checking: Strict mode enforces stricter syntax and rules in JavaScript, which can help to catch errors earlier in the development process. This can save time and effort in debugging code.

  3. Improved performance: The more restrictive nature of strict mode can lead to better performance in some cases. For example, accessing undeclared variables can be slower than accessing declared variables, so enforcing declaration of all variables can lead to better performance.

​

Now take a look at the cons of JavaScript strict mode:

  1. Compatibility issues: Not all JavaScript code is compatible with strict mode, especially older code that relies on certain behaviors that are disallowed in strict mode. This can make it difficult to adopt strict mode in some projects.

  2. More verbose code: Strict mode requires more careful coding practices, which can result in more verbose code. For example, all variables must be declared with the var, let, or const keyword, which can make code longer and harder to read.

  3. Different behavior: Some features of JavaScript behave differently in strict mode, which can lead to unexpected behavior if code is not written with strict mode in mind. This can add complexity to development and testing.

​

Overall, the decision to use strict mode in JavaScript depends on the specific needs and goals of the project. While strict mode can offer some benefits in terms of security, error checking, and performance, it may also introduce compatibility issues and more verbose code.

​

When using 'strict mode' you should test your code and make sure you don't encounter compatibility issues. Then you have to decide if you can live with more verbose code or not.

​

Why don't we end this article by looking at some features that behaves differently in 'strict mode' and in normal mode (non-strict mode).

​

Here is an example we can look at.

​

In non-strict mode, it's possible to create multiple properties with the same name in an object literal by using duplicate property names. The last property with a given name overwrites any previous properties with the same name. Look at this code:

In the code above, the `x` property is defined twice with different values.

In non-strict mode, the second definition overwrites the first one, and the `x` property of `obj` ends up with a value 2.

​

But in strict mode, duplicate property names in an object literal are not allowed. So attempting to create them will result in a syntax error. 

Let's look at how that code will look like.

In the last piece of code, the same object literal that was allowed in non-strict mode will result in a syntax error in strict mode. Because it attempts to define the `x` property twice. This behavior is different from non-strict mode. In 'strict mode' we can potentially catch errors earlier in the development process.

​

Hopefully you now know a little bit better what strict mode in JavaScript is and what the difference between strict mode and non-strict mode is. 

bottom of page