28

I've seen a few places around the internet passively stating 'use strict;' must come on the first line of the functional scope for which you want the behavioral directive to apply.

However, in my experience, it doesn't matter if there are comments before it.

/* some comment */
'use strict';

Is there a functional deficiency with having comments come before the directive, or is it purely a matter of style? Is this defined anywhere in the ECMAScript specification?

I'm asking not only for V8 (node.js) environments, but for browsers as well.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
  • 2
    @DavidHaim Yes. `However, in my experience, it doesn't matter if there are comments before it.` I'm asking if there is a reason not to. Did you read the post? – Qix - MONICA WAS MISTREATED Jul 14 '15 at 17:08
  • there is no reason. when the interperter interprets the code it first throws away any comment it find before continuing on – David Haim Jul 14 '15 at 17:09
  • 7
    @DavidHaim - Even if all browsers work that way it doesn't mean it's spec, and if it's not spec then future browsers may not work that way. He's doing the **right** thing by not just assuming it's spec just because it works in the browsers he's tested. In this case: yes, it is spec. So he's good. But that's not just because he tried it in the browsers and it worked...it's because it's written that way in the spec. – Jimbo Jonny Feb 14 '16 at 16:41
  • 1
    It's also worth noting to the top answers here, that **MDN is _not_ the specification for Javascript**, hence why I didn't accept the highest-rated answer. – Qix - MONICA WAS MISTREATED Sep 06 '16 at 04:55

4 Answers4

22

Yes you can add comments before "use strict";--it just must appear before any statements.

See this example from MDN

// Whole-script strict mode syntax
"use strict";
var v = "Hi!  I'm a strict mode script!";
Dave
  • 10,748
  • 3
  • 43
  • 54
20

According to the ECMAScript 262 5th edition standard section 14.1:

. . . the initial SourceElement productions of a Program or FunctionBody and where each ExpressionStatement in the sequence consists entirely of a StringLiteral token followed a semicolon

Emphasis mine

and the ECMAScript 262 6th edition standard section 14.1.1:

A Directive Prologue is . . . the initial StatementListItem or ModuleItem productions of a FunctionBody, a ScriptBody, or a ModuleBody.

Emphasis mine


Perhaps more simply put, section 7.4 of the ECMAScript 5th ed. standard (section 11.4 of the ECMAScript 6th ed. standard) states:

Comments behave like white space and are discarded

Since comments are not statements and are ultimately discarded, use strict; can come after them as it is the first effective statement in the body.


An update years later - more or less, more context.

Most parsers have a lexing step and a parsing step. The first, lexing, breaks down the textual source code into more palpable tokens (such as left angle bracket, identifier, comma, string literal, etc) and the parsing step actually does something with them - in many cases (and certainly in the case of Javascript), turning it into what's called an Abstract Syntax Tree (or AST for short).

Two things make this "allowed", especially as per this comment - one is the fact that comments are to be discarded, which generally happens during lexing, and the 'use strict'; has to be the first statement, which is generally checked during parsing.

Since lexing comes before parsing, therefore the check for 'use strict'; will never actually see the comments to even be given the chance of having a problem with them.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
  • 2
    It would have been easier to quote 7.4: "Comments behave like white space and are discarded". But since you didn't you'll also have to explain what a `SourceElement` is and what it and a `Directive Prologue` have to do with `use strict`. Links should only serve as reference not as part of the answer. The target of the first link isn't the official specification btw. And with regard to your last sentence: Wouldn't it make more sense to emphasize "statement" instead of "initial" (or both)? – a better oliver Jul 14 '15 at 17:52
  • It would have been easier, but not the full answer. – Qix - MONICA WAS MISTREATED Jul 14 '15 at 17:54
3

According to MDN, use strict; must be the first statement in a script or function. Comments aren't statements.

Matt Olson
  • 409
  • 5
  • 13
0

Yes. Comments may be placed before the "use strict" declaration. JavaScript engines just skip over comments, so for all intents and purposes the "use strict" statement is at the top of the functional scope.

kieranpotts
  • 1,510
  • 11
  • 8