215

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?

3

1 Answer 1

274

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.
10
  • 13
    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 7, 2015 at 15:35
  • 11
    @BrynnMahsman ES2015 has native modules. CommonJS is just a library and has nothing to do with the language.
    – Kit Sunde
    Nov 7, 2015 at 15:54
  • 20
    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 8, 2015 at 16:22
  • 7
    For a detailed examination of "is it a module?", see nczonline.net/blog/2016/04/… "...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 1, 2017 at 12:50
  • 2
    What exactly is a "module"? I get the strict error (with the AirBnB preset) on a script that consists only of "use strict"; console.log('foo'). Is that a module? Jun 23, 2017 at 6:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.