Strict mode

Let’s learn about strict mode and why we use it in JS.

We'll cover the following

Why use strict mode?

Starting from ES5, we can use strict mode for getting more runtime error checking. For instance, in strict mode, all variables must be declared. An assignment to an undeclared variable throws an exception.

We can turn strict mode on by typing the following statement as the first line in a JavaScript file or inside a <script> element:

'use strict';

It’s generally recommended to use strict mode, except when the code depends on libraries that are incompatible.

Get hands-on with 1400+ tech skills courses.