Stay One Step Ahead

Learn JavaScript's modern strict mode and make your code secure.

The traps we saw in the previous lesson have been part of JavaScript for a long time. Even though the language has evolved considerably, these gotchas are inherent, and they’re here to stay. Thankfully, we can make a preemptive strike to avert these issues using some tools and techniques.

Apply the use strict directive

Placing 'use strict'; at the beginning of a JavaScript source file or within a function turns on the strict mode of execution.

In strict mode, the execution runtime does not tolerate the use of undeclared variables, among other things.

Scope of the use strict directive

Let’s discuss the scope of this directive.

  • File scope: If the directive is placed at the top of a script, all code in the script runs in strict mode.
  • Function scope: If placed only within a function, only the code within that function runs in strict mode. The situation where we’ll need this is quite rare.

Get hands-on with 1200+ tech skills courses.