37

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?

It looks like that there's plenty of evidence suggesting that use of semicolons is highly optional and is required in only few of the specific cases.

Bhargav Rao
  • 45,811
  • 27
  • 120
  • 136
Art
  • 22,581
  • 28
  • 84
  • 100
  • 8
    +1 Although when you say *apart from subjective visual perception* I feel you are trying to downplay the importance of sound coding conventions. Because what are coding convention if not helping others to understand your code by appealing to subjective visual perception? – flybywire Mar 08 '10 at 07:58
  • flybywire, good point, but I just didn't want to get this question closed by SO mods as they already did that to the one I opened on python. Hope you do understand. – Art Mar 08 '10 at 08:04
  • Here's a great article that shows there are only like 6 easy to remember situations where you need to use semicolons: http://inimino.org/~inimino/blog/javascript_semicolons – johnsimer Jul 04 '16 at 15:45

5 Answers5

34

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
  • 34,781
  • 17
  • 76
  • 98
  • 22
    Guesses? It follows strict rules, that developers should take 5 minutes to learn and then drop the useless characters if they so choose. Also, the idiot box is the TV... – Ryan Florence Feb 07 '11 at 05:51
  • 3
    great article about js semicolons: http://mislav.uniqpath.com/2010/05/semicolons/ – makevoid Mar 17 '11 at 12:36
  • http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding – Art Nov 14 '11 at 23:28
  • 6
    What a terrible answer. JavaScript interpreters never "guess" where to put a semicolon. Developers might "guess" at the rules of ASI, but that's never a good idea. A developer should *understand* the language they use. – the system Mar 01 '13 at 19:10
  • Its best to know how statements are terminated, not to blindly use semicolons because you "have to" or "someone told you that you should": http://inimino.org/~inimino/blog/javascript_semicolons – B T Jul 15 '13 at 05:51
  • ^ The creator of the language tells us that we should, dummies: https://brendaneich.com/2012/04/the-infernal-semicolon/ – Cliffmeister Mar 03 '14 at 02:04
17

It looks like there are very few reasons, or, actually, edge cases, when one would want to use semicolons.

http://aresemicolonsnecessaryinjavascript.com/ <- this is down now, use

https://github.com/aresemicolonsnecessaryinjavascript/aresemicolonsnecessaryinjavascript.github.com

Art
  • 22,581
  • 28
  • 84
  • 100
  • 1
    +1 ... Good article: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding – the system Mar 01 '13 at 19:32
  • 3
    HTML version of aresemicolonsnecessaryinjavascript.com: http://jsfiddle.net/ju2kqcn2/embedded/result/ – donut Aug 20 '14 at 17:44
13

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 26, point 7.9) to omit it, if it's unambiguous

Boldewyn
  • 78,902
  • 44
  • 148
  • 207
  • 2
    One of the few factual based arguments for proper semi colon use. – Michael J. Calkins Jun 21 '17 at 17:24
  • "why"? This sounds like a religious reason, not something well versed. "Javascript can become complex if you do not use semicolons by default because....", "Python doens't have this problem because...." – paul23 Sep 18 '17 at 09:12
  • 1
    I honestly do not understand, how your comment relates to my answer. I haven't given _any_ personal preference and just quoted the relevant aspects of the languages' own documentation. – Boldewyn Sep 18 '17 at 13:43
11

Because

  • Debugging the subtle bugs that occur when you don't is a waste of time you could be spending doing something cool
  • 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
  • Leaving them out relies on all tools that process JavaScript code in your toolchain getting the rules exactly right (for instance, some minifiers/packers/compressors don't, including Crockford's jsmin, which will break code that relies on ASI in some places)
T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
  • 1
    Can you provide an example of a subtle bug that can occur? I can only think of issues which would immediately throw syntax errors, like a for loop definition. I'd argue that the presence of optional semicolons does more to hurt readability than help it – Charlie Martin Aug 16 '15 at 18:01
  • @CharlieMartin: Well, we'll never be on the same page there. :-) The `return {"foo": "bar"};` example ("Why is this function returning undefined?! Argh!") is well-known to people who pay attention to these things (which most people don't -- fortunately putting an opening brace on its own line is unusual in the JavaScript world so people don't trip over this very often). – T.J. Crowder Aug 16 '15 at 18:08
  • Thanks. I didn't think of that one. I personally consider the linebreak more sinful than the omitted semicolon, but I guess linebreaks should not be able to dictate intention in javascript – Charlie Martin Aug 16 '15 at 18:31
10

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
  • 12,162
  • 6
  • 40
  • 58
  • Douglas Crockford is factually incorrect that "an assignment statement ... must end with a semicolon" so... – B T Jul 15 '13 at 05:53
  • 3
    Crockford isn't being "factual" in his statement. He's giving a suggestion, and the rule you must follow if you're following that suggestion. – worc Oct 24 '13 at 16:21