Strict Mode in JavaScript

JavaScript strict mode tells the interpreter to be more particular about what is allowed. In general terms, strict mode makes your code a little safer by turning some bad coding practices into exceptions.

For existing code, this is good and bad. It’s good, because it helps you find some potential bugs, like accidental globals. It’s bad because code that used to work, might now be broken.

Here’s the kicker: for new code, it is almost always good.

How To Enable It

This part is easy. Strict mode is enabled by placing the literal string “use strict”; at the top of the scope you want to be strict.

That’s it! And the best part, is that it is backwards compatible. In older browsers and interpreters, it degrades to nothing but a literal string. Sure, it won’t enforce strict mode, but it won’t cause any errors, either.

Now let’s take a look at what it does.

Stuff You Don’t Care About

There are a number of things that strict mode does that most people don’t or shouldn’t care about, but it is important to at least know what’s going on.

Removed Support for with Keyword

You shouldn’t be using the with keyword anyway, but in strict mode it is explicitly not allowed.

Don’t read too much into this. Just because the standard calls these “future reserved keywords” does not mean these will be added to the language soon, or at all. Don’t hold your breath.

Cannot override eval or arguments

This is another safety measure. Previously, you could override the eval function. “Replace the built-in eval function? What could go wrong with that!” I digress.

With strict mode enabled, “eval” and “arguments” are not allowed identifiers in user code. This means you cannot create a variable or function with the name “eval” or “arguments”. Since I know you love code, here’s a tiny clip.

Cannot Use arguments.caller or .callee

The ability to access the current executing function, and the function that called it is potentially useful. The main reason this is not allowed in strict mode is for performance.

One of the optimizations that most interpreters use is to in-line functions when possible. This helps performance by reducing the size of the call stack and even (in some cases) reducing the number of run-time closures. When you reference arguments.caller or arguments.callee, this optimization is no longer possible. Combined with the fact that very few people ever use this functionality, it is disallowed in strict mode.

Cannot Use Undeclared Variables

You can still create global variables (add new member to the global object — “window” in a browser), but strict mode forces you to do it explicitly.

Safer Eval

One of the most dangerous and misused features of JavaScript is the eval function. Any string can be executed as code, and it executes in the context of the call to eval. This can lead to some serious security risks.

Strict mode helps to alleviate some of the risks of using eval with two significant changes to the behavior of eval:

Immutable Members Fail Loudly

Property descriptors from ECMAScript 5 gave us the ability to create truly immutable object members. As you work with these, you will surely notice that assigning a non-writable property fails, but fails silently. The only feedback that the assignment failed is the fact that the value didn’t change.

Strict mode causes these assignments to fail loudly. That is, it will throw an exception if you try it.

Note that unlike most strict mode messages, this is a run-time exception. You won’t see it until you run the code.

What Does It All Mean?

In its current incarnation, strict mode is a great tool to help prevent some common JavaScript mistakes and the resulting bugs. It is supported in all modern browsers (yes, even IE), and it degrades gracefully to older browsers without any extra work.

In short… you should be using strict mode in nearly all of your JavaScript code. Do it! Do it now!