The history about VariableStatement

# Felix Kling (10 years ago)

With ES6 having a production rule "Declaration" and ES5 having "FunctionDeclaration", I'm curious why a variable declaration (as we say), officially "VariableStatement", has not been originally defined as a declaration (back then).

Would you consider it to be a declaration today if we didn't need backwards compatibility? Or is there another reason that requires it to be a statement?

# Mark S. Miller (10 years ago)

If it wasn't for backward compatibility, "var" would disappear entirely, leaving only lexical "let" "const" and "function", making VariableStatement disappear as well.

# Brendan Eich (10 years ago)

I took Felix's question to be about grammar naming and factoring conventions, not about var per se.

Since ES1 (ECMA-262 Edition 1, E262.pdf on the ecma-international.org site), the grammar called the nonterminal producing var declarations VariableStatement (ES1 2.2). That was simply for consonance with the other productions reached directly from the Statement nonterminal. It was just a nominal choice, nothing was intended to contradict that var was a declaring form.

From the start, VariableStatement produced right-hand sides with Declaration-based names:

VariableStatement :
var VariableDeclarationList ;

to clue readers in just a bit. Arguably VariableStatement was the better name from "above" in the grammar-structure sense, since it acts like a statement and may occur unbraced even as a sub-statement (e.g. consequent of an if (condition)).

(An even better name from the C family of languages than VariableDeclarationList would be VariableDeclaratorList. The var introduces a list of declarators; the whole is a declaration (which may bind many names, either at top level or via destructuring). An ancient nit I failed to pick in ES1 days.)

ES5 didn't change things much, but ES6 did indeed add Declaration as a new non-terminal produced from StatementListItem. IINM, Felix is asking why when this was done, VariableStatement was not moved to the new Declaration sub-grammar. Allen might want to comment.

# Allen Wirfs-Brock (10 years ago)

On Feb 2, 2015, at 9:46 AM, Brendan Eich wrote:

I took Felix's question to be about grammar naming and factoring conventions, not about var per se.

Since ES1 (ECMA-262 Edition 1, E262.pdf on the ecma-international.org site), the grammar called the nonterminal producing var declarations /VariableStatement/ (ES1 2.2). That was simply for consonance with the other productions reached directly from the Statement nonterminal. It was just a nominal choice, nothing was intended to contradict that var was a declaring form.

Here is the link to obsolete editions of ECMA-262 ecma-international.org/publications/standards/Ecma-262-arch.htm

... ES5 didn't change things much, but ES6 did indeed add /Declaration/ as a new non-terminal produced from /StatementListItem/. IINM, Felix is asking why when this was done, /VariableStatement/ was not moved to the new /Declaration/ sub-grammar. Allen might want to comment.

Mostly because /VariableStatement/ is allowed in all statement contexts (including the then and else clauses of if statements) while /Declaration/ is only allowed in contexts that require a list of statements and declarations.

# Felix Kling (10 years ago)

On 2015-02-02 09:46, Brendan Eich wrote:

That was simply for consonance with the other productions reached directly from the Statement nonterminal.

That is my actual question: Was there a specific reason to make it a /Statement/ as opposed to something similar like /FunctionDeclaration/, which is not a /Statement/?

Mostly because /VariableStatement/ is allowed in all statement contexts (including the then and else clauses of if statements) while /Declaration/ is only allowed in contexts that require a list of statements and declarations.

That I understand.

Sorry if I wasn't able to express my question clearly, let me try it again:

Before ES6, there was basically just one construct that wasn't a /Statement/, namely /FunctionDeclaration/. Now with ES6, let and const have been introduced, but not as /Statement/s (like var), but as /Declaration/s. I assume there is a reason for that? /FunctionDeclaration/ was added to /Declarations/ as well (that one is obvious).

That made me wonder why /VariableStatement/ was a /Statement/ to begin with? Maybe the concept of a "declaration" wasn't really developed back then, but then what was the reasoning for /FunctionDeclaration/ not being a /Statement/ (was it even part of ES1? I don't know) ?

I would accept the answer "because Brendan had to develop the language in such a short time and that's just the way he did it" ;)

# Brendan Eich (10 years ago)

Felix Kling wrote:

That made me wonder why /VariableStatement/ was a /Statement/ to begin with? Maybe the concept of a "declaration" wasn't really developed back then, but then what was the reasoning for /FunctionDeclaration/ not being a /Statement/ (was it even part of ES1? I don't know) ?

Yes, see ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262, 1st edition, June 1997.pdf -- anyone can check.

In ES1, functions were only top-level, per (14):

Program :
SourceElements

SourceElements :
SourceElement
      SourceElements SourceElement

SourceElement :
Statement
      FunctionDeclaration

That's why FunctionDeclaration was separate from the start.

I would accept the answer "because Brendan had to develop the language in such a short time and that's just the way he did it" ;)

The ES grammar reflects what was in "JS1", indeed.