

Are Semicolons Necessary in JavaScript? - jashkenas
http://aresemicolonsnecessaryinjavascript.com/

======
mustpax
Omitting semicolons in JavaScript is like omitting braces from one line if
blocks.

It's all well and good until someone turns this:

    
    
      if (conditional)
        statement;
    

Into this without realizing what they've just done:

    
    
      if (conditional)
        statement;
        statement2;
    

Yes, you can skip semicolons. And yes it will work the vast majority of the
time. But the amount of time you save will be eclipsed by that one time
someone makes a change to the code that breaks in an unpredictable subtle way
due to your purely aesthetic shorthand.

Writing maintainable code is about learning humility. There's no need to show
off your understanding of the deeper points of JavaScript semicolon insertion
in your production code base. Just insert the semicolons, I promise you you
will learn to not be distracted in no time.

~~~
dionidium
I've never understood this argument for always including braces in one-line
if-blocks. Won't this error be immediately obvious? I mean, after making the
change, won't even the most minimal test catch the mistake?

If developer #2 isn't doing the most basic test to verify that "statement2"
runs when "conditional" is true, then you've got way bigger problems than your
style guide.

~~~
mustpax
Anecdotally I've seen smart and capable developers make this mistake numerous
times. It usually gets caught by code review, automation etc. But not always.

It's basically the birthday paradox. You take the probability of a very
unlikely event and raise it to a big enough power (enough people, enough lines
of code) it becomes a non-negligible outcome.

------
flyosity
Good luck minifying/compressing your JavaScript files without using
semicolons. There's nothing I hate more than finding a promising jQuery plugin
and when I integrate it into our JS minifying processes it blows up because
they left semicolons off of a few lines, or the whole thing.

~~~
roryokane
I’ve never personally minified JavaScript, with or without semicolons, but the
article “Semicolons in JavaScript are optional” linked to in the page says the
author minified jQuery without semicolons with Google Closure Compiler, and
the test suite passed. Is that article wrong? Did minifying semicolons use to
work, but they removed that feature for some reason? Does no one actually use
Closure Compiler, and the other minifiers don’t support missing semicolons? Or
is the reason the plugin you found “blows up” simply the error described in
“Semicolons in JavaScript are optional” in the section “The only real pitfall
when coding without semicolons”, which is starting a line with “(d + e)”
instead of “;(d + e)”?

That article says that if a minifier doesn’t understand JavaScript that an
actual JavaScript interpreter can understand, the minifier is buggy. I agree
with it – if your minifier messed up the semicolon-less jQuery plugin, and if
none of the situations I mentioned above was the case, then your minifier
needs fixing, or you should start using a different one.

------
selectnull
To the author of the website: you have modified google analytics code and
removed semicolons, I congratulate you on that. It shows true conviction. :)

Joke aside, that's a good content put together in very clear and concise
manner.

------
paulirish
Unless you get a perfect score, don't omit semicolons <http://asi.qfox.nl>

Oliver Hunt, primary JavascriptCore engineer took this quiz and said, "I
rewrote our entire JS parser < 2 months ago, with all ASI rules and still
didn't get 100% on a quiz"

~~~
jacobolus
Most of those examples are designed to push the edge cases in confusing ways
(i.e. bad code). Easy answer: don’t do that.

Even if you include semicolons in all the “reasonable” places for them, it’s
quite possible to get tripped up if you don’t understand what counts as a
statement in JS and what doesn’t.

Edit to add: For example, don’t write code like this, with or without the
autoinserted semicolons:

    
    
      var x = function()
      {
        return (function(n){
          return n
        })(n)
      }(function(){Math.exp(
        f
      )})
    

Someone else looking at the code is hardly going to be better off
understanding it if you change that to:

    
    
      var x = function()
      {
        return (function(n){
          return n;
        })(n);
      }(function(){Math.exp(
        f
      );});

------
tedunangst
The semicolons are optional people really seem like they are picking a fight
just to pick a fight. Why not explain how my JS will be _better_ if I omit
some semicolons and not just the same?

~~~
roryokane
I would think they didn’t explain how it makes JS better because they were on
the defensive from people who said leaving semicolons out will break code.
They are saying “stop forbidding me from coding in the style I want, my style
works just fine”. They are just trying to defend their style, not convert
people to it.

However, that’s not to say there is no reason to omit semicolons. I personally
leave out semicolons as an extension of the DRY principle – if you are putting
a line break after a statement anyway, then you might as well leave off the
semicolon. It’s one less character to delete if you combine two adjacent lines
into one. Also, if you already think of line breaks and statement ends going
together, then semicolons are just noise that you have to make an effort to
ignore – the effort required may be small, but there’s little reason not to
avoid that effort.

The only problem with omitting semicolons in JavaScript is this case mentioned
in “Semicolons in JavaScript are optional”:

    
    
      a = b + c
      (d + e).print()
    

The author recommends avoiding the error in that by changing the last line to

    
    
      ;(d + e).print()
    

. If the extra effort of adding a semicolon before all line-starting
parentheses costs more effort than you would save by getting the two
advantages I described above, then maybe adding semicolons everywhere is right
for you. You might have to experiment with coding in both styles to be able to
accurately quantify how much those two advantages and one disadvantage would
affect you.

------
beej71
I think ECMA-262-5 7.9 pretty concisely sums it up... and at the same time
there are plenty of nasty examples in the "FUD" section at the bottom of the
article.

The spec very clearly says they may be used, so I trust no one will have a
problem with me using them. It's tough to drop that old C habit, anyway. Even
my Python code has a random scattering of semicolons at the ends of lines.

------
jpadvo
I always use semicolons to end lines in JavaScript because it is comfortable
for me.

...and I always feel a little guilty about it, because it seems wrong to add
superfluous characters to my code. Is it actually "personal preference" and
I've been feeling guilty for nothing, or is it better to leave off the
semicolons?

~~~
roryokane
See my reply to someone else, describing the advantages and disadvantages of
omitting semicolons: <http://news.ycombinator.com/item?id=2184289>

As for whether you should omit the semicolons, only you can decide how much
those pros and cons affect you. Depending on how much you care about others
who read your code, you might also want to consider how you think those other
people would choose, if they evaluated those pros and cons for themselves.

------
__Joker
Slightly out of topic
[https://groups.google.com/forum/?pli=1#!msg/nodejs/MWaivVTir...](https://groups.google.com/forum/?pli=1#!msg/nodejs/MWaivVTirPY/0pnRjKsggkIJ)

------
serichsen
The problem is not the semicolons, it is the newlines, and there is little you
can do about that. The newline/semicolon issue is the most ridiculous language
design decision ever.

------
drivebyacct2
I wish they were. It can cause unexpected problems.

<http://bonsaiden.github.com/JavaScript-Garden/#semicolon>

(this really wasn't the example that I was thinking of, I'll scrounge for the
other examples, sorry)

~~~
jacobolus
That example has nothing to do with whether semicolons are omitted or not; the
rule that you need to know is _don’t ever put a newline between the “return”
keyword and the value to be returned_ , but whether you add semicolons at the
end of what you want to make a statement makes absolutely no difference in
this case.

In other words, whether you write

    
    
      return
        foo;
    

or

    
    
      return
        foo
    

the result is the same: an empty return, completely ignoring the foo bit.

~~~
drivebyacct2
Right, but if it wasn't "autoinserting" semicolons, the behavior _would_ be
different.

~~~
jacobolus
That doesn’t have any bearing on whether human programmers should add
unnecessary semicolons to lines where there is no such ambiguity.

