0

A student asked me why JavaScript requires semicolons after variable declarations but not after function declarations and I didn't really have a good answer.

For example, these variable declarations (including the one holding a function) are followed by semicolons...

var x = 5;
var test = function() { return null; };

But this function declaration has no semicolon afterwards nor should it. Why? What is the logic behind the differentiation? Why does variable assignment require a semicolon but function declaration does not?

function test {
    return null;
}
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • *"variable assignment require a semicolon"* - it doesn't a new line separates statements in the same way a semicolon does. If you remove the `;` on the lines in your first code snippet it would still work fine. – Spencer Wieczorek Oct 02 '15 at 21:25
  • Technicality, irrelevant. Strict mode would not allow that. – temporary_user_name Oct 02 '15 at 21:26
  • 1
    Please justify your downvotes, people. Lmao. Gotta love SO sometimes. As if this is off-topic, too broad, a rant, etc. – temporary_user_name Oct 02 '15 at 21:28
  • 1
    @Aerovistae The downvotes were deserved. Most of the early answers were seriously wrong. –  Oct 02 '15 at 21:29
  • @Aerovistae. Seriously. I got downvoted for giving a completely correct answer. That stuff should be moderated more closely. – durbnpoisn Oct 02 '15 at 21:29
  • Downvotes for the *question,* not the answers. – temporary_user_name Oct 02 '15 at 21:29
  • 1
    @durbnpoisn Your answer stated incorrectly that a statement of the form `var test = function() {…}` was a function declaration, and thus did not require a semicolon. –  Oct 02 '15 at 21:31
  • What I stated was that the syntax was wrong anyway. And it was. – durbnpoisn Oct 02 '15 at 21:32
  • You don't need a semicolon after `if`, `for`, `while`, `catch`... either – Amit Oct 02 '15 at 21:34
  • 1
    @durbnpoisn You claimed that the "correct syntax" would require newlines before and after the outer braces of the function declaration. This is false; newlines are not required here. –  Oct 02 '15 at 21:35
  • 1
    Aerovistae, my guess is that the question got downvoted because, rather than taking for granted syntax and asking a question about how to code something, or about semantics, etc., you asked about the underlying rationale and logic of the syntax requirements of the language. You asked about something that most questions take as given. The question therefore might feel too abstract and "opinion-based" to some people--but I think they're wrong. This is about the logic of the language--and languages, since what justifies Javascript syntax might be used in other languages. – Mars Oct 02 '15 at 21:35
  • No kidding - asking about the grammar of a programming language is VERY on topic. I could find a hundred examples to prove this. – temporary_user_name Oct 02 '15 at 21:38
  • Your question is coming from a false notation that semi-colons are required when they are not in your cases. Peraphs you should re-state the question on why a function declaration `test(){ ... }` does not need to be separated by a `;` **OR** a new line. – Spencer Wieczorek Oct 02 '15 at 21:40

2 Answers2

1

Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement.

Basil Baby
  • 101
  • 6
  • 1
    You dont need semicolon for the first one either – Patrick Evans Oct 02 '15 at 21:29
  • Welcome to Stack Overflow, Basil! It's ok to quote other answers like you've done here (http://stackoverflow.com/a/1834674/502381) but you should always provide reference back to where you've taken the quote. – JJJ Oct 02 '15 at 21:30
  • OHH a function definition is not a statement. Bam. I knew that and yet wasn't thinking of it that way. Of course. The fact that it isn't a statement is virtually the central axiom of the language, allowing everything we do with anonymous functions. Okay. – temporary_user_name Oct 02 '15 at 21:30
  • I just showed an example. – Basil Baby Oct 02 '15 at 21:31
  • @Juhana I don't see any overlap between the answers. He didn't copy that. – temporary_user_name Oct 02 '15 at 21:31
  • @Aerovistae The entire answer is copied from the fifth paragraph of the answer I linked to. – JJJ Oct 02 '15 at 21:32
  • @Juhana you got me, didn't see it. Shucks, didn't see that entire question. This is a duplicate. Close it. – temporary_user_name Oct 02 '15 at 21:33
  • That's not correct though. If you look at the grammar, some statements are terminated by semicolons (like an ExpressionStatement) and some are not, like the IfStatement. – Felix Kling Oct 02 '15 at 23:12
1

Well, semicolons after function deceleration aren't required.

The answer is pretty simple. Semicolons are used in JavaScript on order to separate statements. So function declarations aren't statements.

Edit: Oh, @Basil Baby, was faster :P

NeoTrix
  • 124
  • 8