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?

Sean McMillan
  • 10,058
  • 6
  • 55
  • 65

1 Answers1

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.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 3
    I am somewhat surprised to see that generators are not implicitly strict. – Sean McMillan Mar 27 '15 at 15:51
  • 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örre Jul 09 '15 at 10:56
  • @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. – Bergi Jul 09 '15 at 17:22
  • 4
    What constitutes "module code"? Use of `export` anywhere in the file, or `import` also? – greim Oct 07 '15 at 16:46
  • 4
    @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. – Bergi Oct 07 '15 at 18:42
  • I see. So if it's valid to have the `import` keyword in your code—even if you don't *actually* have it—then that entire piece of code is forced into in strict mode. And whether it's valid depends on how it's being loaded and/or who's loading it. – greim Oct 07 '15 at 22:23
  • @greim No, this is the wrong way to put it. As Bergi mentioned, it's not the source of the included file that decides if the file is a module or not, it completely depends on how the external environment executes the file. For example, if an external JS file loads your file using the import statement, your file is executed as a JS module. If you load your file via a – mgol Apr 27 '16 at 18:24
  • @m_gol I don't think this captures it either. I've been following the back-and-forth for how Node will determine what's a module, and however they implement it, you'll be able to `import` things that aren't modules, and `require()` things that are modules (in Node, not sure about browsers). The actual mechanism Node will use to determine what constitutes a module (ie .mjs versus package.json) is still being hashed out. See: https://www.nczonline.net/blog/2016/04/es6-module-loading-more-complicated-than-you-think/ . But it's fairly settled that browsers will use ` – greim May 14 '16 at 16:13
  • @greim I think my description is correct, that's just how ES6 modules work. When you'll be doing `import './foo'` in a Node ES6 module then most likely the Node module loader will first check if foo resolves to an ES6 module or an old-style Node one and in the latter case will import from an ad-hoc created shim module that translates the old stuff into the new. From the ES6 point of view, you can only import ES6 modules. – mgol May 16 '16 at 23:16