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?
-
1`while { } do` and `for () { }` have this property too. – Ja͢ck Jun 11 '13 at 03:53
-
This is about the language grammar. You know what that is, look for "Statements" [here](http://www-archive.mozilla.org/js/language/grammar14.html). – acdcjunior Jun 11 '13 at 03:56
-
[When should I use a semicolon after curly braces?](http://stackoverflow.com/questions/2717949/when-should-i-use-a-semicolon-after-curly-braces) – Ja͢ck Jun 11 '13 at 03:57
-
5This is a question about grammar of a language; I do not see why this should have been closed as non-constructive. – Jacob Lee Jan 07 '16 at 15:19
-
Readers: if you think this should not have been closed, then click re-open. – Ira Baxter Apr 16 '16 at 12:38
-
2Readers: Wow, that worked :-} Question is no longer closed. – Ira Baxter Apr 18 '16 at 05:15
3 Answers
- 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.

- 25,981
- 23
- 80
- 125

- 11,075
- 4
- 33
- 54
-
1I 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
-
2I 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
-
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.

- 93,541
- 22
- 172
- 341
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 ;

- 5,992
- 6
- 47
- 83

- 24,500
- 3
- 42
- 63