Please consider the official ECMAScript specification as the source for your answer, and not a document published by a specific browser vendor. (I am aware of Mozilla extend
I do not agree with the other answers that say it is valid.
According to the ECMA-262 5th Edition specification, Blocks
can only contain Statements
(Section 12.1):
Block :
{ StatementList opt }
StatementList :
Statement
StatementList Statement
However the spec does not define a function statement, but only a FunctionDeclaration
and a FunctionExpression
. The spec goes further to make a note on this in Section 12:
Several widely used implementations of ECMAScript are known to support the use of
FunctionDeclaration
as aStatement
. However there are significant and irreconcilable variations among the implementations in the semantics applied to suchFunctionDeclarations
. Because of these irreconcilable difference, the use of aFunctionDeclaration
as aStatement
results in code that is not reliably portable among implementations. It is recommended that ECMAScript implementations either disallow this usage ofFunctionDeclaration
or issue a warning when such a usage is encountered. Future editions of ECMAScript may define alternative portable means for declaring functions in aStatement
context.
For further reading, you may also be interested in checking out the comp.lang.javascript FAQ Section 4.2:
4.2 What is a function statement?
The term function statement has been widely and wrongly used to describe a
FunctionDeclaration
. This is misleading because in ECMAScript, aFunctionDeclaration
is not aStatement
; there are places in a program where aStatement
is permitted but aFunctionDeclaration
is not. To add to this confusion, some implementations, notably Mozillas', provide a syntax extension called function statement. This is allowed under section 16 of ECMA-262, Editions 3 and 5.Example of nonstandard function statement:
// Nonstandard syntax, found in GMail source code. DO NOT USE. try { // FunctionDeclaration not allowed in Block. function Fze(b,a){return b.unselectable=a} /*...*/ } catch(e) { _DumpException(e) }
Code that uses function statement has three known interpretations. Some implementations process
Fze
as a Statement, in order. Others, including JScript, evaluateFze
upon entering the execution context that it appears in. Yet others, notably DMDScript and default configuration of BESEN, throw aSyntaxError
.For consistent behavior across implementations, do not use function statement; use either
FunctionExpression
orFunctionDeclaration
instead.Example of FunctionExpression (valid):
var Fze; try { Fze = function(b,a){return b.unselectable=a}; /*...*/ } catch(e) { _DumpException(e) }
Example of FunctionDeclaration (valid):
// Program code function aa(b,a){return b.unselectable=a}
I'm not sure how to read this, but ECMA-262 V5 has this to say:
NOTE Several widely used implementations of ECMAScript are known to support the use of
FunctionDeclaration
as a Statement. However there are significant and irreconcilable variations among the implementations in the semantics applied to such FunctionDeclarations. Because of these irreconcilable difference, the use of a FunctionDeclaration as a Statement results in code that is not reliably portable among implementations. It is recommended that ECMAScript implementations either disallow this usage of FunctionDeclaration or issue a warning when such a usage is encountered. Future editions of ECMAScript may define alternative portable means for declaring functions in a Statement context.
If I understand this correctly, strictly speaking, this means that function declarations can't be inside blocks at all, because Blocks can contain only Statements.
I can be totally wrong with my interpretation, though - I am not familiar with the internal workings of ECMAScript.
From ECMA 262 chapter 14
- Program Syntax
Program : SourceElements SourceElements : SourceElement SourceElements SourceElement SourceElement : Statement FunctionDeclaration Semantics
The production Program : SourceElements is evaluated as follows:
Process SourceElements for function declarations.
Evaluate SourceElements.
Return Result(2).
The production SourceElements : SourceElement is processed for function declarations as follows:
- Process SourceElement for function declarations.
The production SourceElements : SourceElement is evaluated as follows:
Evaluate SourceElement.
Return Result(1).
The production SourceElements : SourceElements SourceElement is processed for function declarations as follows:
Process SourceElements for function declarations.
Process SourceElement for function declarations.
The production SourceElements : SourceElements SourceElement is evaluated as follows:
Evaluate SourceElements.
If Result(1) is an abrupt completion, return Result(1)
Evaluate SourceElement.
Return Result(3).
The production SourceElement : *Statement is processed for function* declarations by taking no action.
The production SourceElement : *Statement is evaluated as follows:*
1. Evaluate Statement.
2. Return Result(1).
The production SourceElement : FunctionDeclaration is processed for function declarations as follows:
- Process FunctionDeclaration for function declarations (see clause 13).
The production SourceElement : FunctionDeclaration is evaluated as follows:
- Return (normal, empty, empty).
The awnser is officially NO. (Šime Vidas convinced me the hard way in another question)
But no Exception is specified either so it fails or works silently depending on browser implementations.
In the ECMAScript standard, a FunctionDeclaration is not defined as a Statement, and a Statement is not defined as being able to contain a FunctionDeclaration, so they are not compatible according to the standard (though in practice every JavaScript interpreter will attempt to do something sensible, albeit not consistent between implementations).
Yes it is valid.
All statement blocks (i.e everything within curly braces) can have additional statements and declarations, including functions.
So you can also define functions within functions and so on
Here is ECMA-262 v1 - http://www.mozilla.org/js/language/E262.pdf
No, it's invalid. Function declarations can only appear as "source elements", which are either in the global scope or immediately within another function definition, outside all other statements. From the ECMA-262 spec:
FunctionBody : SourceElements
Program : SourceElements
SourceElement : Statement | FunctionDeclaration
There is no other production in the grammar that allows a FunctionDeclaration
.
Only function expressions are allowed to be part of a statement:
MemberExpression : FunctionExpression
...
Statement : ExpressionStatement
Edit: There was a related discussion on another question recently. See the comments on this answer - earlier, I too thought that this could be valid but the grammar makes it clear that it is invalid.