Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created October 19, 2012 19:39
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rwaldron/3920242 to your computer and use it in GitHub Desktop.
Save rwaldron/3920242 to your computer and use it in GitHub Desktop.

Strict Mode: The Summary!

Identifiers (variables, functions) cannot be named any of these: "implements", "interface", "let", "package", "private", "protected", "public", "static", and "yield"

OctalLiterals are not allowed.

Accidental global declarations will throw:

(function() {
  "use strict";

  oops = "this will throw";
}());

ReferenceError: oops is not defined

Assigning "eval" is not allowed:

(function() {
  "use strict";

  var eval = "throws";
}());

SyntaxError: Variable name may not be eval or arguments in strict mode

arguments.caller and arguments.callee are not allowed.

Arguments objects don't dynamically share their array indexed property values with the corresponding formal parameter bindings.

(function( a, b, c ) {
  "use strict";

  // formal parameter bindings
  console.log( a, b, c );

  arguments[0] = "ka-boom";

  // a is unchanged
  console.log( a, b, c );

}( "a", "b", "c" ));

Assigning "arguments" is not allowed:

(function( a, b, c ) {
  "use strict";

  var arguments = "will throw";

}( "a", "b", "c" ));

SyntaxError: Variable name may not be eval or arguments in strict mode

It is a SyntaxError if strict mode code contains an ObjectLiteral with more than one definition of any data property (11.1.5).

(function() {
  "use strict";

  var o = {
    p: "val",
    p: "other"
  };
}());

SyntaxError: Duplicate data property in object literal not allowed in strict mode

Formal parameters cannot be named "eval" or "arguments"

(function( eval, arguments ) {
  "use strict";
}( "a", "b" ));

SyntaxError: Parameter name eval or arguments is not allowed in strict mode

Formal parameters cannot be duplicated in name

(function( a, a ) {
  "use strict";
}( "a", "b" ));

SyntaxError: Strict mode function may not have duplicate parameter names

eval cannot create variables or functions in the variable environment that eval was called in:

(function() {
  "use strict";
  eval("var a = 'alpha';");
  console.log( a );
}());

ReferenceError: a is not defined

"this" value will not be coerced to a global object when not explicitly defined or implied by new construction:

(function() {
  "use strict";

  function f() {
    // will be 'undefined'
    console.log( this );
  }

  f();
}());

The delete operator is restricted to object properties

(function( a ) {
  "use strict";

  delete a; // throws
  delete arguments[0]; // throws

  function foo() {}

  delete foo; // throws

  var b = "beta";

  delete b; // throws

}( "a" ));

SyntaxError: Delete of an unqualified identifier in strict mode. (same for all three)

Object properties that are [[Configurable]]:false cannot be deleted

(function() {
  "use strict";

  var o = {};

  Object.defineProperty( o, "p", {
    value: "thing",
    configurable: false
  });

  delete o.p;
}());

TypeError: Cannot delete property 'p' of #

with statements are not allowed.

"eval" and "arguments" may not be used as the name of the error object in a try catch:

(function() {
  "use strict";

  try {
    a();
  } catch( eval ) {
    console.log( eval );
  }
}());

SyntaxError: Catch variable may not be eval or arguments in strict mode

Implementors are not allowed to make up additional strict mode rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment