2

It seems like a stupid question but I really can't wrap my head around it. I began to wonder this when I was thinking of why there is a semicolon after variable declaration but not function declaration. So does that mean a function declaration is an expression?

And what is the official meaning for statement? I've been finding the answer saying that a statement is a "command" that tells computer what to do, but isn't a function declaration telling the computer what to do? Does it have something to do with when it's get loaded when executing the code?

10
  • See the language spec for a definition of statement. Commented Jun 10, 2014 at 17:36
  • named function statements are (basically) converted to top-of-program var assignments to function expressions of the same name. if you can assign something to a var, it's an expression, it that errors, it's a statement (in general). also, statements generally affect the flow, expressions generally don't. Commented Jun 10, 2014 at 17:57
  • @dandavis: named function statement??? Commented Jun 10, 2014 at 18:04
  • function gt(n){return n>this;} is a statement, gt=function(n){return n>this;}; is an expression. both define functions. OOP'rs often don't like the statement form, preferring methods or expressions. Commented Jun 10, 2014 at 18:07
  • 1
    @dandavis: No, function gt(n){return n>this;} is not a statement. See my answer. We have function declarations and function expressions. The latter can exist in a named and anonymous form. Function declarations always need a name. This might sound like splitting hairs, but terminology is important (hello JSON object!). Commented Jun 10, 2014 at 18:13

2 Answers 2

2

I began to wonder this when I was thinking of why there is a semicolon after variable declaration but not function declaration

Not every statement ends with a semicolon.

Examples of statements with trailing semicolon:

  • Variable declaration: var foo = <expression>;
  • Expression statement: <expression>;
  • Do while loop: do <statement> while (<expression>);

Examples without trailing semicolons:

  • If statement: if (<expression>) <statement>
  • Try catch statement: try <block> catch (<identifier>) <block>
  • For loop: for (...) <statement>
  • Block statement: { <statement list> }

So does that mean a function declaration is an expression?

No, it's more complicated than that. Function declarations are not statements. They are not expressions either, they are source elements. Statements and function declarations are both source elements. **


And what is the official meaning for statement?

I can't tell you the official definition, but in the context of JS I would say something like "it's an instruction that does not produce an assignable result/value". Expressions on the other hand produce a result that can be used in other expressions.


** Good to know, but a bit off-topic: Since function declarations are not statements, they are technically not allowed to be used inside blocks. This becomes even more apparent if we also consider hoisting. This example should throw a syntax error in every browser, but unfortunately it doesn't.

if (true) {
  function foo() { alert('foo'); }
} else {
  function foo() { alert('bar'); }
}
foo();

This leads to different behaviors in different browser. While Chrome will show bar, Firefox will show foo. Chrome just hoists both function declarations, and the second overrides the first one. Firefox interprets both declarations as something like a function expression.

Try it yourself in different browsers.

Sign up to request clarification or add additional context in comments.

5 Comments

You only forget the completions statements (break/continue/return/throw), the empty statement and the debugger; statement :-)
@Bergi: There are more than I thought :)
Honestly, I would never have thought of the do while loop; I always omit that one. ASI FTW!
You should do that again. I just found the following in the spec: "The result of evaluating a Statement is always a Completion value." Even though that's internal of course, it might be useful for further explanation of what they do
@Bergi: Yeah, but you can't use that in code (not assignable). I considered it to be more like the ReferenceType, it's just internal to the specification. But I might be wrong.
2

A variable declaration can have side-effects (eg, var x = alert(42);).

A function declaration cannot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.