What Is JavaScript Strict Mode?

The “strict mode” pragma

Since ECMAScript 5 (JavaScript 1.8.5) JavaScript developers have been able to place their code (down to a specific file or function) in a strict operating context, known as “strict mode”. I first came across this when using jslint many moons ago with it’s “missing ‘use strict’ pragma” error message.

The strict operating context is (go on, guess) more strict, by throwing exceptions under stricter guidelines and by preventing certain actions.

How to use it

You invoke strict mode by placing the string “use strict” at the top of your JavaScript file or function.

use strict example
"use strict";

Why use it

All it does it enforce good practices, here are a few examples:

Duplicate properties

Duplicate properties become a thing of the past. A syntax exception will be thrown if you attempt to declare a duplicate.

The “with” statement

Forget it (if you ever knew about it). It’s bad practice and the strict operating context abolishes it, it will throw a syntax exception.

Variable declaration

You must always declare your variables before assigning them. This is good practice anyway and should always be done to prevent random global variables that can be accessed in any scope.

A note on browsers

Implementation of strict mode in browsers is limited and likely to change with future iterations, it’s best to test your JavaScript (as always) in all browsers.

Let’s end on a laugh

The strict operating context won’t spoil your fun running this:

"use strict";
alert(Array(16).join("Robin"-1) + "Batman");