Strict Mode

ES5 added a “strict mode” to the language, which tightens the rules for certain behaviors. Generally, these restrictions are seen as keeping the code to a safer and more appropriate set of guidelines. Also, adhering to strict mode makes your code generally more optimizable by the engine. Strict mode is a big win for code, and you should use it for all your programs.

You can opt in to strict mode for an individual function, or an entire file, depending on where you put the strict mode pragma:

  1. function foo() {
  2. "use strict";
  3. // this code is strict mode
  4. function bar() {
  5. // this code is strict mode
  6. }
  7. }
  8. // this code is not strict mode

Compare that to:

  1. "use strict";
  2. function foo() {
  3. // this code is strict mode
  4. function bar() {
  5. // this code is strict mode
  6. }
  7. }
  8. // this code is strict mode

One key difference (improvement!) with strict mode is disallowing the implicit auto-global variable declaration from omitting the var:

  1. function foo() {
  2. "use strict"; // turn on strict mode
  3. a = 1; // `var` missing, ReferenceError
  4. }
  5. foo();

If you turn on strict mode in your code, and you get errors, or code starts behaving buggy, your temptation might be to avoid strict mode. But that instinct would be a bad idea to indulge. If strict mode causes issues in your program, almost certainly it’s a sign that you have things in your program you should fix.

Not only will strict mode keep your code to a safer path, and not only will it make your code more optimizable, but it also represents the future direction of the language. It’d be easier on you to get used to strict mode now than to keep putting it off — it’ll only get harder to convert later!

Note: For more information about strict mode, see the Chapter 5 of the Types & Grammar title of this series.