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, a FunctionDeclaration is not a Statement; there are places in a program where a Statement is permitted but a FunctionDeclaration 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, evaluate Fze upon entering the execution context that it appears in. Yet others, notably DMDScript and default configuration of BESEN, throw a SyntaxError.

For consistent behavior across implementations, do not use function statement; use either FunctionExpression or FunctionDeclaration 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} 


Other FAQ Resources: