30

In ECMAScript 5 (aka JavaScript,) I can trigger strict mode by adding "use strict" at the top of my function (or file, but this is discouraged.)

I understand that in ECMAScript 6, certain syntax features will turn on strict mode, especially class, and modules (however you do those.)

In the ECMAScript 6 world, what is the complete list of ways to trigger strict mode?

2
  • i'm guessing es6 is all strict mode. Commented Mar 26, 2015 at 16:33
  • That was the original plan, but backwards compatibility concerns mean the spec still contains a lot of support for "lax mode." Commented Mar 26, 2015 at 16:34

1 Answer 1

32

The spec says:

  • Module code is always strict mode code.
  • All parts of a ClassDeclaration or a ClassExpression are strict mode code.

The rest are just the known things from ES5, basically every global/eval/function code that begins with the "use strict"; directive. It does work within the new ES6 function kinds (arrow, generator, method syntax) as well.

Sign up to request clarification or add additional context in comments.

9 Comments

I am somewhat surprised to see that generators are not implicitly strict.
The answer and the spec (linked by someone above) are missing one crucial piece of information: What if I use ES 2015 but no ES 2015 modules? Because I'm using io.js and the new module system cannot be used yet (unless I use Babel), so I do use ES 2015 features but wait until I can actually natively use the new module syntax. Is strict mode implied in this scenario or not? If not they should really say "ES 2015 modules". It's only relevant for node.js/io.js since in the browsers it's clear, but node.js already has modules....
@MörreNoseshine: As the answer says, the rules for strict mode in non-modules are just the same as in ES5. Node ("module") scripts still need to explicitly begin with "use strict" to make the module scope IEFE use strict mode.
What constitutes "module code"? Use of export anywhere in the file, or import also?
@greim: yes, both export and import declaration make it a module, they would be syntax errors elsewhere. But whether a javascript code file is interpreted as a script or as a module, only your transpiler/engine decides, you cannot know it by looking upon the code.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.