209

I'm not familiar with ECMAScript 6 yet. I've just cloned the React Starter Kit repo, which uses ES6 for application code. I was surprised to see that the linter is configured to forbid occurences of the use strict directive, which I thought was recommended in pre-ES6 JavaScript. So what's the point?

Midiparse
  • 4,441
  • 4
  • 25
  • 44
  • 20
    [ES6 modules and classes are strict by default.](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-strict-mode-code) – Ry- Jul 28 '15 at 19:22
  • 2
    related: [Which ECMAScript 6 features imply strict mode?](http://stackoverflow.com/q/29283935/1048572) – Bergi Jul 28 '15 at 19:59

1 Answers1

264

ES6 modules are always in strict mode. To quote the relevant part of the spec:

10.2.1 Strict Mode Code

An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. Code is interpreted as strict mode code in the following situations:

  • Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).
  • Module code is always strict mode code.
  • All parts of a ClassDeclaration or a ClassExpression are strict mode code.
  • Eval code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct eval (see 12.3.4.1) that is contained in strict mode code.
  • Function code is strict mode code if the associated FunctionDeclaration, FunctionExpression, GeneratorDeclaration, GeneratorExpression, MethodDefinition, or ArrowFunction is contained in strict mode code or if the code that produces the value of the function’s [[ECMAScriptCode]] internal slot begins with a Directive Prologue that contains a Use Strict Directive.
  • Function code that is supplied as the arguments to the built-in Function and Generator constructors is strict mode code if the last argument is a String that when processed is a FunctionBody that begins with a Directive Prologue that contains a Use Strict Directive.
Kit Sunde
  • 32,665
  • 22
  • 111
  • 176
  • 11
    What's the difference between global code and module code? I think I am misunderstanding, as module code to me means everything in module.js if I require('module'). – May Oakes Nov 07 '15 at 15:35
  • 8
    @BrynnMahsman ES2015 has native modules. CommonJS is just a library and has nothing to do with the language. – Kit Sunde Nov 07 '15 at 15:54
  • 18
    Thanks for your response. So is it not an ES6 module if it doesn't use the import/export keywords? Right now I'm using Node 4 and I don't have access to import/export keywords and I'm just using the CommonJS module.exports and require() along with the ES6 features enabled by default. It would explain why I have to put use strict at the top of every file. So technically I'm still writing CommonJS modules with some ES6 features enabled in V8? – May Oakes Nov 08 '15 at 16:22
  • 7
    For a detailed examination of *"is it a module?"*, see https://www.nczonline.net/blog/2016/04/es6-module-loading-more-complicated-than-you-think/ *"...while the presence of import or export might indicate a module, the lack of import or export does not clearly indicate that the file is not a module. So there is no effective way to autodetect that a file is a module during parsing."* – ptim Feb 01 '17 at 12:50
  • 2
    What exactly is a "module"? I get the `strict` error (with [the AirBnB preset](https://www.npmjs.com/package/eslint-config-airbnb-base)) on a script that consists only of `"use strict"; console.log('foo')`. Is that a module? – Dan Dascalescu Jun 23 '17 at 06:45
  • @DanDascalescu no, you aren't exporting anything. Type "es2015 module" into google. – Kit Sunde Jun 23 '17 at 07:08
  • @KitSunde: Right. So why does eslint flag that `"use strict"` as an error? `--print-config` shows `"strict": "error"`. – Dan Dascalescu Jun 23 '17 at 10:05
  • @BrynnMahsman ES6 Modules are completely messed up idea, unfortunately, landed to standard, popularized by Babel, and "implemented" in Chrome and Safari. https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c – Brian Cannard Dec 22 '17 at 01:39
  • @KitSunde I think your answer could be improved by summarizing the comments regarding modules – jcollum Oct 22 '20 at 19:00