0

The following line is known as a function expression in JavaScript. Does it contain within it a function declaration?

var foo = function() {};

Edit: edited for clarity.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • Sorry, but that is not a duplicate. It is related, but not a duplicate. – Ben Aston Apr 08 '15 at 17:46
  • Both are declaring functions and the difference is explained in the dup. If you think your question is different than the dup , then edit your question and clarify what else you are asking. – jfriend00 Apr 08 '15 at 17:53
  • @jfriend00 That may be so, but there is a lot of (uncorrected) misinformation in responses to that "dupe" too. "Function declaration" there is conflated with "function statement" throughout. Hence my confusion and this question. – Ben Aston Apr 08 '15 at 17:56
  • So this whole question is about spec terminology and not result? What is the actual objective of the question? – jfriend00 Apr 08 '15 at 18:25
  • @jfriend00 Is understanding the terminology not an end in itself? – Ben Aston Apr 08 '15 at 18:29

3 Answers3

3

No, it does not. Both function expressions and function declarations do define functions, and contain the function keyword.

How they differ is basically determined where they appear in the source code, their syntax is otherwise the same. It is

  • a function expression when it appears where an expression is expected, e.g. within the grouping operator or an assignment expression (as in your code)
  • a function declaration when it appears directly in the body of a function, module, script (global code), or eval code. Since ES6, declarations can also appear as part of a StatementList, i.e. inside a block.
  • a function statement when it appears where a statement is expected. Until ES6, this term was used to distinguish it from a declaration when the function syntax appeared in a block. If not inside a block, it is basically equivalent to a declaration.

Also read http://kangax.github.io/nfe/ which explain all these terms (pre-ES6 though).

For the difference between their evaluations see var functionName = function() {} vs function functionName() {}.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • OK I presume this is defined in the spec. Are function statements similarly well-defined (I'll hunt now)? Edit: it would appear not. – Ben Aston Apr 08 '15 at 18:07
  • OK thanks very much. This is *much* clearer than the reams of high-voted answers elsewhere on here. – Ben Aston Apr 08 '15 at 18:09
  • I am unable to find "function statement" in the ES5 spec... https://es5.github.io/#x12 – Ben Aston Apr 08 '15 at 18:10
  • @BenAston: Your question would've been answered by some of the lower-voted answers on that suggest duplicate, though. See the answers by CMS, thomasrutter and T.J.Crowder especially. – Bergi Apr 08 '15 at 18:13
  • OK point taken, but when in a position of ignorance, teasing the information from the misinformation (or at least substantial ambiguity) is by definition impossible. – Ben Aston Apr 08 '15 at 18:16
  • @jfriend00: I didn't mean that `var f = function() {}` is a function statement. That's an expression statement containing a function expression in an assignment expression of course. – Bergi Apr 08 '15 at 18:17
  • 1
    @BenAston: Yes, "function statement" is not an ECMAScript term. I think it was dubbed by mozilla. See http://kangax.github.io/nfe/#function-statements for more information. – Bergi Apr 08 '15 at 18:18
  • 2
    @BenAston For the origin of "function statement", see the "NOTE" at the bottom of [section 12 of ES5](http://www.ecma-international.org/ecma-262/5.1/#sec-12): "Several widely used implementations of ECMAScript are known to support the use of *FunctionDeclaration* as a *Statement*..." – apsillers Apr 08 '15 at 18:25
3

We can readily determine that this is not function declaration, according to ES5 section 14:

  • Program :

    • SourceElementsopt
  • SourceElements :

    • SourceElement
    • SourceElements SourceElement
  • SourceElement :

    • Statement
    • FunctionDeclaration

A FunctionDeclaration can only be a SourceElement (a quick search for FunctionDeclaration shows no other grammatical use), and a SourceElement can only be a top-level component of a list of SourceElements (either a Program or a FunctionBody). This use of function is nested inside of an assignment, so it cannot be a top-level SourceElement.

Furthermore, we can also rule out this as a FunctionDeclaration by its definition:

  • FunctionDeclaration :

    • function Identifier ( FormalParameterListopt ) { FunctionBody }

Your code does not have an Identifier, which is mandatory for FunctionDefinitions (but optional for FunctionExpressions).

apsillers
  • 112,806
  • 17
  • 235
  • 239
1

A Function Expression defines a function but does not declare a function. As such the code

var foo = function() {};

defines a function and then assigns it to a variable.

Where as

function foo() {};

defines a function and declares it without the need for an assignment.

bhspencer
  • 13,086
  • 5
  • 35
  • 44