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?
-
22ES6 modules and classes are strict by default.– Ry- ♦Jul 28, 2015 at 19:22
-
2related: Which ECMAScript 6 features imply strict mode?– BergiJul 28, 2015 at 19:59
-
I think there's something very misleading about ES6 claim of being strict by default. They simply aren't and many things that would be otherwise caught by 'use strict' are silently ignored. This requires some more clarification.– PawelApr 6, 2022 at 15:33
1 Answer
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.
-
13What'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'). 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. Nov 7, 2015 at 15:54
-
20Thanks 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? Nov 8, 2015 at 16:22
-
7For 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."– ptimFeb 1, 2017 at 12:50
-
2What 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