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.