Function Declaration :
function Identifier ( FormalParameterList opt ){ FunctionBody }
Function Expression :
function Identifier opt ( FormalParameterList opt ){
FunctionBody }
If a function
foo(){}
is part of, say, assignment expression, it is considered a function expression. If, on the other hand, function
foo(){}
is contained in a function body or in a (top level of) program itself — it is parsed as a function declaration.
A somewhat less obvious example of function expression is the one where function is wrapped with parenthesis — (function
foo(){})
.
The reason it is an expression is again due to a context: "(" and ")" constitute a grouping operator and grouping operator can only
contain an expression.