40

I understand that it is good syntax to use semicolons after all statements in Javascript, but does any one know why if/else statements do not require them after the curly braces?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Ronathan
  • 594
  • 1
  • 4
  • 10

3 Answers3

56
  • Semicolon is used to end ONE statement
  • { and } begin and close a group of statements

Basically, an if-else must be followed by either a statement or a group of statements.

if-else followed by a statement:

if (condition) statement;
if (condition); // followed by a statement (an empty statement)

if-else followed by group of statements:

if (condition) {
   statement;
   statement;
}

if (condition) {
   // followed by a group of statements of zero length
}

if-else must end with a ; if it is followed by a single statement. if-else does not end with a ; when followed by a group of statements because ; is used to end a single statement, and is not used for ending a group of statements.

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
invisal
  • 11,075
  • 4
  • 33
  • 54
  • 1
    I disagree with "even if-else must end with ;". Any statement in javascript need not end with ; FYI. There is no strict rule as such. – Vineeth Pradhan Jun 11 '13 at 03:59
  • Any statement that is not contextualy terminated (ie by curly braces) needs a ; – Orangepill Jun 11 '13 at 04:08
  • @Orangepill JavaScript doesn't have that strict rule, but it's recommended anyway. – Ja͢ck Jun 11 '13 at 04:08
  • @Jack Right, you'll need semicolon in that case. Didn't think about coding in that fashion – Vineeth Pradhan Jun 11 '13 at 04:10
  • @jack Are you referring to Javascript's semicolon insertion rules? Note the term is semicolon insertion, even if you don't put them there the parser does. – Orangepill Jun 11 '13 at 04:11
  • @Orangepill That technical detail doesn't matter to the argument though. – Ja͢ck Jun 11 '13 at 04:16
  • @jack True.. the discussion is centered around language syntax and semicolon insertion is a language "feature". – Orangepill Jun 11 '13 at 04:20
  • 2
    I did not aware of "Semicolon Insertion" (didn't even know it exists), but I found it is a little silly for us to remember extra rules just to omit one semi-colon once every five to ten lines. Or maybe there is advantage of using it that I am not aware of. – invisal Jun 11 '13 at 04:23
  • 1
    @invisal I whole heartedly agree. I think it was put there as a means to be forgiving to non-programmers early on but instead it tends to just mask bugs by making programs run that would otherwise error out. – Orangepill Jun 11 '13 at 04:30
  • @invisal Yes. That is exactly why I asked. I'm new to javascript and I found it very odd that certain statements required semicolons whereas some did not. I just automatically adds semicolons after every curly brace for consistency in my code. – Ronathan Jun 17 '13 at 19:34
  • Where are the `else`'s in the code examples? – steveOw Nov 30 '20 at 00:25
14

The real answer is because many modern languages copied their syntax from C, which has this property. JavaScript is one of these languages.

C allows statement blocks

 { ... }

(which don't need terminating semicolons) to be used where statements can be used. So you can use statement blocks as then- and else- clauses, without the semicolons.

If you place a single statement in the then- or else- clause, you'll need to terminate it with a semicolon. Again, just as in C, with the extra JavaScript twist that ; is optional at the end of a line, if inserting it would not cause a syntax error.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
8

Because the curly braces themselves are termination characters.

They are tokens that enclose a compound statement block and are intrinsically terminated. It's like putting a period at the end of a sentence, it signals to the parser that the thought is complete.

While being completely ugly it is valid to wrap every statement in {} and omit the ;

nCardot
  • 5,992
  • 6
  • 47
  • 83
Orangepill
  • 24,500
  • 3
  • 42
  • 63