

Why are programmers afraid of not using semicolons in JavaScript? - einaregilsson
http://programmers.stackexchange.com/questions/99195/why-are-programmers-afraid-of-not-using-semicolons-in-javascript-but-dont-care-i

======
jannes
There are some caveats. This, for example, would not work as expected:

    
    
      foo()
      [1,2,3].forEach(bar)
    

Automatic semicolon insertion wouldn't work here, because the bracket could be
a continuation of the last line. The same goes for parentheses at the
beginning of a line.

Source: [http://blog.izs.me/post/2353458699/an-open-letter-to-
javascr...](http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-
leaders-regarding)

~~~
sjs
The informal and single easy rule is to prefix lines that begin with
punctuation with a semi. In practice only [ and ( crop up, almost nobody
begins lines with a prefix increment or decrement. If you use += 1 and -= 1
instead you sidestep that case.

~~~
felipemnoa
Or, you could just add a semicolon. KISS at work.

~~~
einaregilsson
Yes, you could just add a semicolon in front of lines that start with
punctuation (tiny percentage of all lines) or you could just stick them at the
end of every single line...

~~~
einaregilsson
@eropple: So you put semicolons after every line? Even after block statements
{} ? Why not, if the goal is to be consistent?

~~~
burgerbrain
You should use the reply button.

~~~
einaregilsson
It wasn't visible for me on that one comment. I don't know why, I thought it
was too deply nested or something, but now I see it. Weird.

~~~
sjs
The Reply link doesn't show up for a minute or two. I guess it's supposed to
force you to think instead of firing off a hasty response. Kind of pointless
for one liners but hey...

------
evilswan
"...but you have to be aware of some edge cases"

There's the reason.

~~~
einaregilsson
But isn't that part of learning any language? There are plenty of weird edge
cases in other languages but we get used to them. Basically this boils down
to:

"When you have a single statement, that stretches out over more than one line,
but those lines are legal statements on their own, then it won't work like you
want it to"

I would guess that the number of statements that stretch out over many lines
are a very small part of the overall number of statements for most code.

But it just seems like most developers think that javascript might insert
semicolons all over the place. Which it won't, the rules are clear once you
learn them.

~~~
felipemnoa
Or, just add a semicolon and keep it simple. Why risk it? KISS at work.

~~~
einaregilsson
There are two possibilities:

1\. Prefix lines that start with punctuation, specifically [(-+/ with a ;.
Then omit them everywhere else. Lines that start with punctuation are a tiny
subset of all lines generally.

2\. Postfix every line with a ;, "just in case"

I can't see how nr. 1 is harder. And in case 2 you don't actually put ; at the
end of every line, you don't put them after {} blocks for example, so it's not
like it's somehow easier to remember.

"Because we're used to doing it that way" is a reason I guess, but not a great
one in my opinion.

------
haberman
Google's JavaScript style guide requires explicit semicolons; rationale:
[http://google-
styleguide.googlecode.com/svn/trunk/javascript...](http://google-
styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Semicolons)

~~~
bluesnowmonkey
The examples being put forth in defense of requiring semicolons are really
weak.

    
    
      // 1.
      MyClass.prototype.myMethod = function() {
        return 42;
      }  // No semicolon here.
      
      (function() {
        // Some initialization code wrapped in a function to create a scope for locals.
      })();
    

There are at least two potential solutions here: 1) put a semicolon after
every function declaration, and 2) put a semicolon _before_ every self-
executing function. Both work fine. Either way, you have to remember to put a
semicolon somewhere, right? No difference in cognitive load. And if you say
that #1 is easier to remember because it's more C-like, you've just blown your
cover. You don't actually write C at your day job, do you? Because otherwise
you'd realize that C does not require semicolons after functions.

    
    
      [normalVersion, ffVersion][isIE]();
    

Same thing. Just put a semicolon before lines that start with arrays like
this. Also this code is crap.

    
    
      -1 == resultOfOperation() || die();
    

OK hold up, who really writes this stuff? Is this what code looks like at
Google? Forget semicolons for a second and let's work on basic code
readability. Explain the line aloud. "If resultOfOperation returns negative
one, then die." Now write it.

    
    
      if (resultOfOperation() == -1) die();
    

Sweet, now people can read our code without juggling operator precedence rules
in their head. Minor side effect: the semicolon issue goes away.

    
    
      return
      { foo: bar }
    

Really? Why oh why would you write such a thing and expect it to return the
object?

~~~
einaregilsson
Exactly! The semicolon prefix can be put even more simply: Prefix any line
that starts with punctuation with a ;

There, you've just fixed all the cases that are likely to go wrong.

------
cam-
When compressing javascript down, missing a semi-colon can cause broken js,
especially in older IE browsers. Trailing commas on js object literals would
also freeze an entire page in IE. When that happened it would often take
several days in jslint trying to work out where the broken js was from.

~~~
bluesnowmonkey
If that's the case, then the compression tool isn't parsing the Javascript
correctly.

~~~
sjs
Watch out or all the nutjobs who uses Crockford's broken compressor will come
out of the woodwork to defend it.

------
hippich
my biggest concerns - why people want to _not_ use semicolons? Does it improve
load speed may be? or make code more readable or help with something else?

~~~
bluesnowmonkey
Readability. Python, Ruby, and CoffeeScript are more readable for their
parsimonious approach to punctuation, aren't they? We like those languages,
right?

~~~
hippich
I am not in your "we".

Also, JS was not designed from start as semicolon-less language. If you prefer
semicolon-less JS - use Coffee Script. Otherwise it is very hard to debug such
code for someone who do not expect semicolon-less JS code.

------
hsmyers
If memory serves, IBM's PL1 used to insert missing punctuation that it (the
compiler) felt was missing. This wasn't particularly well thought out and
often caused problems that were very difficult to debug. I used to think of it
as 'doing me a favor that I hadn't asked for' and grew to hate both the
feature and the language (the language for other reasons). In the years since
then I've tried to be consistent in avoiding any such feature. I find it much
easier to go ahead and use what ever the language needs. If I use semi-colons,
then I don't have to guess there after...

~~~
russell
In several years of PL/I programming, the compiler correctly inserted a semi-
colon exactly once and then aborted the compile. I remember because it was
such a surprise.

------
hullo
Haven't seen mentioned yet that .html files and legacy tools have an extremely
lax historical attitude towards whitespace. People of a certain age came to
the web understanding that a space is the same as a tab is the same as a line
feed, as far as browsers are concerned.

The (increasingly common) practice on minifying is just one offspring of that
history, which is, to the main point, not shared with Python.

------
ScottBurson
In general, DWIM (Do What I Mean) is evil. We're better off being forced to
say exactly what we mean than having the machine try to figure it out for us.
This lesson has been learned repeatedly over the years by programming language
designers -- though clearly, alas, not by everyone.

------
Jach
This is probably furthest down on my list of worries when writing JS...
Sometimes I use semicolons, sometimes I don't (I try to be consistent in the
same file), and when I'm in somebody else's house I follow their style. The
examples given for why I should always use semicolons (e.g. the return + value
on different line) are things that just don't come up for me and usually look
ugly enough I'd avoid them anyway. (Also they're fairly simple to discover if
you're actually testing your code.) The last time I did a return and value on
the next line was in Java...

------
JohnLBevan
You should always endeavour to make your code easy to read. Since most people
aren't aware semi colons are optional, omitting them can cause unexpected
behaviour in certain cases, and having them there does no harm, it's surely
best to stick to the standards (i.e. using semi colons). The only time to
debate whether to omit them is when there's a good reason to leave them out.
Also, I suspect (untested theory) that it puts more strain on the parser,
though makes negligible difference to the download speed, so will be a hit on
performance.

------
ethagnawl
I got into the habit of using ;s to avoid having "my feelings hurt" by JSLint:

    
    
        Problem at line 1 character 10: Expected ';' and instead saw '(end)'.

~~~
adatta02
Dropping semicolons also breaks a good deal of minifiers.

~~~
sjs
No, it really does not. Try it on any minifier that actually parses the JS and
not some subset of the language.

------
benbeltran
All this discussion just makes me wonder: Why are some other programmers
afraid of using them? It's just a semicolon.

------
sjs
Reason #1: Every time it comes up everyone who uses semicolons but doesn't
really know why pipes up with their misguided and misinformed views on why
dragons will eat your heart if you leave them out. Just look at the comments
here.

~~~
notSorella
My feelings exactly :3

------
snorkel
Force of habit and clarity. Sometimes it's necessary, like "return i++" is
ambiguous.

~~~
einaregilsson
return i++ is not ambigous. It will return the current value of i, e.g. if i
was 2 it will return 2, not 3. But that's just how the post increment operator
works (in every language that I know of), that has nothing to do with
semicolon insertion.

~~~
eropple
"return i++" is not ambiguous, on that you are correct--but it _is_ terrible
practice because it lends itself to being misconstrued.

Writing clear code is superior to writing "cute" code, and "return i++" is a
case of the latter. In any case where the increment actually matters you are
directly affecting a shared state and such changes should be made contextually
obvious to the state of the object--one's neck would need to be very, very
beard to think that this is immediately obvious and apparent in a contextually
valid way.

Similarly, for many programmers, myself included, a semicolon is read as a
period is in English--the end of the statement. (You keep saying that "but
people don't get annoyed at Python when it does it," and guess what? I
_certainly_ get annoyed at it--Python consistently frustrates me because I
find it considerably less readable.) Omitting semicolons is less immediately
clear than including them and, again, _can be misconstrued_. Only it can be
misconstrued by the program interpreter, too. (Oops.)

~~~
einaregilsson
Well, at least we agree that 'return i++' is terrible practice (although it
has nothing to do with semicolons) :)

But when you write Python code, do you then end every line with a ; ? To make
it more readable for yourself?

~~~
eropple
I prefer to avoid Python entirely, but there are cases where I have to fiddle
with preexisting code or the nature of the problem makes more sense to use it,
i.e. a library already exists that doesn't elsewhere. In those cases, I write
idiomatic Python, without semicolons, because That's The Way You Write Python.

Consistency is important, and the Python community has essentially said "don't
use semicolons." The JavaScript community has essentially said "use semicolons
aggressively". I write code according to those idioms so other people are able
to more easily use it.

------
jpr
That's a stupid question to ask. More appropriate would be: who on earth
thinks that omitting semi-colons makes anything better?

~~~
bluesnowmonkey
Yukihiro Matsumoto and Guido van Rossum come to mind.

~~~
daeken
It's pretty obvious from the context that the OP was referring to omitting
semicolons in Javascript, not in other languages.

~~~
bluesnowmonkey
Yes it is. My point is that when those guys created their respective
languages, they omitted semicolons. One would surmise that they thought it
made things better.

~~~
burgerbrain
This discussion is explicitly about javascript. How _javascript_ benefits from
including or excluding semicolons.

