views:

2066

answers:

9

In many situations, JavaScript parsers will insert semicolons for you if you leave them out. My question is, do you leave them out? I'll post simple Yes/No answers as a poll, but I'm also interested in your reasons.

If you're unfamiliar with the rules, there's a description of semicolon insertion on the Mozilla site. Here's the key point:

If the first through the nth tokens of a JavaScript program form are grammatically valid but the first through the n+1st tokens are not and there is a line break between the nth tokens and the n+1st tokens, then the parser tries to parse the program again after inserting a virtual semicolon token between the nth and the n+1st tokens.

That description may be incomplete, because it doesn't explain @Dreas's example. Anybody have a link to the complete rules, or see why the example gets a semicolon? (I tried it in JScript.NET.)

This stackoverflow question is related, but only talks about a specific scenario.

+49  A: 

Yes, you should use semicolons after every statement in JavaScript.

This answer and its opposite are intended as a poll. Vote for the one you agree with, and then post an answer with your reasons if you like.

Don Kirkby
If a language has certain cases where a semi-colon is required, I would take that and apply it to all cases if possible simply for the sake of code that is easier to read and code that simply looks good. So... +1.
Dalin Seivewright
For the sake of consistency and future readability, include semicolons after every statement. +1
JustinT
Reasoned discussion is better than taking sides.
ShreevatsaR
See Crockford's reasons here: http://javascript.crockford.com/code.html"JavaScript allows any expression to be used as a statement. This can mask some errors, particularly in the presence of semicolon insertion."
Jordan Liggitt
Why hope that the JS interpreter will correctly guess your intentions (not just now, but with minor code changes) when you can just add the semicolon and communicate them unambiguously?
Andrzej Doyle
A: 

No, only use semicolons when they're required.

This answer and its opposite are intended as a poll. Vote for the one you agree with, and then post an answer with your reasons if you like.

Don Kirkby
+12  A: 

Yes, you should always use semicolons. Why? Because if you end up using a JavaScript compressor, all your code will be on one line, which will break your code.

Try http://www.jslint.com/, it will hurt your feelings, but show you many ways to write better JavaScript (and one of the ways is to always use semicolons).

Ryan Doherty
If your javascript compressor can break your javascript code, I'd recommend using another one as soon as possible.
Alsciende
+7  A: 

I'd say consistency is more important than saving a few bytes. I always include semicolons.

On the other hand, I'd like to point out there are many places where the semicolon is not syntactically required, even if a compressor is nuking all available whitespace. e.g. at then end of a block.

if (a) { b() }
Jimmy
In that particular case you save a few bytes *and* use a semicolon with if (a) b;
Kev
fine :) "if (a) { b() c() }"
Jimmy
+7  A: 

I think this is similar to what the last podcast discussed. The "Be liberal in what you accept" means that extra work had to be put into the Javascript parser to fix cases where semicolons were left out. Now we have a boatload of pages out there floating around with bad syntax, that might break one day in the future when some browser decides to be a little more stringent on what it accepts. This type of rule should also apply to HTML and CSS. You can write broken HTML and CSS, but don't be surprise when you get weird and hard to debug behaviors when some browser doesn't properly interpret your incorrect code.

Kibbee
+4  A: 

JavaScript automatically inserts semicolons whilst interpreting your code, so if you put the value of the return statement below the line, it won't be returned:

Your Code:

return
5

JavaScript Interpretation:

return;
5;

Thus, nothing is returned, because of JavaScript's auto semicolon insertion

Andreas Grech
The classic example. +1
meouw
This is true, but is not at all relevant to the question. Explicitly using a semicolon would not help in this scenario.
Joel Anair
It would, as it would make the problem obvious on first sight of the code (alright more obvious, a true javascript programmer will consider it obvious without semicolons as well)
Jasper
+1  A: 

I use semicolon, since it is my habit. Now I understand why I can't have string split into two lines... it puts semicolon at the end of each line.

+3  A: 

What everyone seems to miss is that the semi-colons in JavaScript are not statement terminators but statement separators. It's a subtle difference, but it is important to the way the parser is programmed. Treat them like what they are and you will find leaving them out will feel much more natural.

I've programmed in other languages where the semi-colon is a statement separator and also optional as the parser does 'semi-colon insertion' on newlines where it does not break the grammar. So I was not unfamiliar with it when I found it in JavaScript.

I don't like noise in a language (which is one reason I'm bad at Perl) and semi-colons are noise in JavaScript. So I omit them.

staticsan
+12  A: 

An ambiguous case that breaks in the absence of a semicolon:

// define a function
var fn = function () {
    //...
} // semicolon missing at this line

// then execute some code inside a closure
(function () {
    //...
})();

This will be interpreted as:

var fn = function () {
    //...
}(function () {
    //...
})();

We end up passing the second function as an argument to the first function and then trying to call the result of the first function call as a function. The second function will fail with a "... is not a function" error at runtime.

Ates Goral
This is a good example of a real-world case where a semicolon is needed.
Joel Anair