views:

504

answers:

7
+17  Q: 

Why use semicolon?

Are there any reasons, apart from subjective visual perception and cases where you have multiple statements on the same line, to use semicolon at the end of statements in Javascript?

+11  A: 

Yes. Internet Exploder will die horribly when omitting them.
It might also create trouble when minifying the Script.
Apart from that: Code Convention.

EDIT: adding these for reference:

Gordon
There are many reasons to do proper semicolon placement that have nothing to do with partisanship and Microsoft-hating. It is far more than mere code convention (as the many other answers on this page can attest to!).
JUST MY correct OPINION
@ttmrichter I don't hate MS and I gave another practical reason (minification). Also, I never said the list is complete, did I? Apparently some people felt this answer is helpful, otherwise it would have gotten the upvotes it got.
Gordon
@Gordon, could you expand the list perhaps?
Art
@Art No, sorry. I stated the first three things that came to my mind. I wouldn't know what to add now what wasn't mentioned in the other answers already. This page *as a whole* answers your question very nicely. I think this is much more important than having one single comprehensive answer.
Gordon
@Gordon, thanks for links!
Art
+1 for Internet Exploder
v01d
+5  A: 

As Douglas Crockford suggests -

Put a ; (semicolon) at the end of every simple statement. Note that an assignment statement which is assigning a function literal or object literal is still an assignment statement and must end with a semicolon.

N 1.1
+17  A: 

Because JavaScript does nasty things to you when it guesses where to put semicolons. It's better to be explicit and let the interpreter know exactly what you meant than it is to let the idiot box guess on your behalf.

References:

...and a cast of thousands.

JUST MY correct OPINION
+5  A: 

Technically speaking, your code will never work in obfuscators and/or minifiers; otherwise, because it is a part of the language.

Automatic semi-colon insertion is a gross handicap that almost everyone agrees should have never been included in the language.

Justin Johnson
+4  A: 

Because

  • Debugging the subtle bugs that occur when you don't is a waste of time you could be spending doing something cool
  • Minifiers/packers/compressors rely on it
  • It makes it clearer to someone maintaining the code later what you intend
  • Not all code maintainers understand the rules for automatic insertion well enough to maintain code with them left out
  • Not all implementations get the rules for automatic insertion quite right
T.J. Crowder
+2  A: 

If you asked, because you come from a Python background: The difference is:

  • in Python you shouldn't terminate lines with anything, but are allowed to use the semicolon, if you must

  • in JavaScript you should terminate the lines with a semicolon, but are allowed (PDF, page 25, point 7.9) to omit it, if it's unambiguous

Boldewyn
A: 

yep, it's better to let javscript know than to let it decide for a variety of reasons, but browser compatibility is the most prominent one.

Nick