DEV Community

Cover image for Be strict: 'use strict';!
guennithegun
guennithegun

Posted on

Be strict: 'use strict';!

The strict mode of JavaScript was introduced with ECMAScript 5. With the strict mode you can opt in to a restricted variant of JavaScript to write better JS programs.

Using strict mode results in several changes to normal JS:

  • it eliminates some silent errors by changing them to throw errors
  • code in strict mode can sometimes be made to run faster than identical code written in sloppy mode
  • prohibits some syntax likely to be defined in future versions of ECMAScript

How to opt in strict mode (opt out sloppy mode)?

You can opt in strict mode per program (means per file!) like this:

// only whitespace or comments allowed before strict mode
'use strict';

// run some code in strict mode

The strict mode snippet must appear on top of the file and only whitespace and comments are allowed in front of it.

You can turn on the strict mode in per-function scope, too.

function whoIsJohnGalt() {
  // only whitespace or comments allowed before strict mode
  'use strict';

  // run some code in strict mode
}

Note that the function-level strict mode is disallowed if the program (file) has strict mode turned on on top. So you have to choose between one or the other.

The only reason to use the per-function scope is when you want to convert an existing sloppy mode file and need to make the changes little by little over time. Otherwise use strict mode for the whole program.

A third way to enter strict mode is using JS modules introduced by ECMAScript 2015. JS modules are automatically in strict mode, with no statement needed to initiate it.

function whoIsJohnGalt() {

  // I am strict by default because I am a module

}

export default whoIsJohnGalt;

Why use strict mode?

You should not think of strict mode as a restriction to write JS code. The purpose is more being a guide to the best way to do things so that the JS engine has the best chance of optimizing and efficiently running the code.

As most JS programs are written in developer teams, strict mode helps collaboration on code and avoids mistakes that slip by in sloppy mode. For example strict mode makes it impossible to accidentally create global variables.

'use strict';

let globalVariable = 'Who is John Galt?';

globalVarible = 'I am John Galt.';

// this will throw a ReferenceError because of the typo
// without 'use strict'; this would create a new variable globalVarible

So strict mode is like a linter reminding you how JS should be written to have the highest quality and best chance at performance.

Examples

Let's look on some examples on what strict mode does not allow (list is not complete).

Using a variable or object without declaring it:

'use strict';

// variable
x = 10;  

// object
y = { key: 'value' };

// both would cause an error

Duplicating a parameter name is not allowed:

'use strict';

function x(p1, p1) {};   

// will cause an error

Octal numeric literals and escape characters are not allowed:

'use strict';

let x = 010;

let y = '\010';

// both would cause an error

Deleting an undeletable property is not allowed:

'use strict';

delete Object.prototype;

// will cause an error

Deleting a variable (or object) is not allowed:

'use strict';

let x = 30;

delete x;

// will cause an error

Writing to a read-only property is not allowed:

'use strict';

let obj = {};

Object.defineProperty(obj, "x", { value: 0, writable: false });

obj.x = 3.14;            

// will cause an error

There are some more cases that strict mode does not allow. For more examples and more details visit the MDN web docs.

Strict mode as default?

If strict mode is strongly recommended why is it not set to default for JS is a question that could come up. The answer is the backwards compatibility of JS. That means that code which once was accepted as valid JS, there will not be a future change to the language that causes that code to become invalid JS.

Think again of the examples shown above. If a JS engine update started assuming code was strict mode even if it's not marked as such and for example you used variables without declaring them, this may cause that the code would break as a result of strict mode controls.

Conclusion

Along with tooling like linters, strict mode is an other important piece for writing clean and and more "secure" JS. You should definitely use strict node in your code. If your code is not written in strict mode you should think about a transition into strict mode. This article gives you some guidance on this topic.

Happy coding!

Oldest comments (0)