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
  • 50,140
  • 28
  • 121
  • 140
Art
  • 23,747
  • 29
  • 89
  • 101
  • 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

35

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
  • 35,674
  • 17
  • 77
  • 99
  • 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
  • 7
    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
  • @Cliffmeister It's an informative article, but the only advice I see from him regarding the issue is "be careful not to use ASI as if it gave JS significant newlines," i.e, ASI follows strict rules that, if used carefully, can be used effectively in JS, which is what other comments have said. – General Grievance Aug 23 '22 at 16:44
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
  • 23,747
  • 29
  • 89
  • 101
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
  • 81,211
  • 44
  • 156
  • 212
  • 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
12

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
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @T.J.Crowder That's more of an argument to put open braces on the same line than it is about semicolons, though, isn't it? JS puts the semicolon at the linebreak after a `return` regardless of whether there is one at the end of the object literal or not. The only way to solve that (assuming open braces always get their own lines) is to open parentheses on the same line as the `return` and put the literal inside the parens. – General Grievance Aug 23 '22 at 16:55
  • @GeneralGrievance - Yes, the `return` thing wasn't a good example at all, I've removed that comment. Here's a better example: `const fn = function() { console.log("fn") } (() => { console.log("inline") })()`. I got hit by one of those just recently in a project where they don't use any format-on-save tool. (If they did, I'd like to think I'd've noticed the weird reformatting. :-) ) – T.J. Crowder Aug 24 '22 at 07:56
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,418
  • 6
  • 43
  • 61
  • 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