Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Semicolons in JS is how you differentiate between someone who understands that code ambiguity leads to pain, vs someone who shouldn't be allowed near multi-line statements.


Honest question: how often does the code ambiguity due to lack of semicolons lead to non-obvious bugs? I'm nowhere close to being a JS expert, but I've done some JavaScript-heavy pet projects and I've got an ambiguous statement interpreted wrong exactly once, and the cause was pretty obvious within a minute of looking at the error.


In my experience, precisely as often as you write statements that start with a left bracket or paren. E.g.

    [1, 2, 3].forEach( ... )
    (function(){ ... })()
Statements like that are virtually always errors under ASI, but if you avoid them nothing else causes problems.


It's not so much how often it happens, but how freaking hard it is to find the problem by inspection when it does happen.


Linters correctly highlight all the ASI-related that realistically happen. (Or eslint does anyway, can't speak for others.)


Good linters correctly highlight all missing semicolons as a symptom of sloppy, negligent, careless programming (or pointless syntactic showboating). It's also technically syntactically valid to omit braces around single statements after if, else, for, and while statements, but idiotic and dangerous to do that, too.

https://wiki.c2.com/?FixBrokenWindows

https://www.rtuin.nl/2012/08/software-development-and-the-br...

https://blog.codinghorror.com/the-broken-window-theory/

https://medium.com/@matryer/broken-windows-theory-why-code-q...


If you accidently write a comma instead of semicolon it can have interesting effects.


Please give a concrete example of this pain that you have actually experienced. I understand that ASI (automatic semi-colon insertion) is in theory ambiguous because you are relying on the compiler, or in some cases many different compilers, to execute the statement correctly. In practice, for over a decade, I have never run into such an issue. I suspect it's due to engineers trying to get too cute with their implementations.


Crockford on Bootstrap's semicolon omission: “insanely stupid code” (github.com)

https://news.ycombinator.com/item?id=3842713

https://github.com/twbs/bootstrap/issues/3057

And don't forget to semicolon-harden your most important mission-critical code:

https://www.bluejava.com/4PN/Semicolons-in-JavaScript---The-...

    ;;;;;;;;;;;; // protective wall of semicolons
    ;;var a = 10, b = 20;;
    ;;
    ;;function add(a,b)
    ;;{;;
        ;;return a + b;;;;;;;; // this function MUST work
    ;;};;
    ;;;;;;;;;;;; // protective wall of semicolons


Without opening your console, consider whether the below code throws or not and why:

  var a = []
  var b = ''
  var c = {}
  [].concat.apply(a, [b, c])


;[]... is one of two rules you memorize when you omit semis. It's not even that common of a case, though your own example doesn't even make sense.

I don't really understand the drama, just like people who use semis in place you don't need them. In real code, you should be using a linter anyways. At which point semis or no semis becomes purely an aesthetic concern. Like whether you use single or double quotes.

I haven't used semicolons in over 8 years and never had an issue. And with tools like eslint and prettier, you have no excuse to be making such trivial mistakes, like adding superfluous semicolons like function(){};


I have other more important rules I need to memorize with those brain cells, that I don't need to voluntarily choose to give myself more problems that require me to memorize ridiculous rules and obscure special edge cases to look out for all the time, that inexplicably screw me if I happen to slip up and forget to apply them. I stopped playing "step on a crack, break your mother's back" years ago, when I was old enough to actually have somewhere I needed to be.


A typical response is

  return

  false
Where the program returns undefined. I can only think of these examples as straw men. Not once did I run in to an ASI bug.


Note though, that this ambiguity doesn't really affect the whole "whether to use semicolons" debate either way. If anything, it's more likely to occur in code that does use semicolons than in code that doesn't.


Quite right. As do other examples.


Thank you. I have yet to see an example in this thread or anywhere else that did not fall into this category.


    foo=arr[i]


    (counter++)


    if(foo)
      doX()
      doY()


Intuitively this becomes:

if(foo) doX() doY();

Intuitively, as a someone who uses C style languages, this becomes:

if(foo) doX(); doY();

Can someone confirm which intuition is correct?

I ask because, to me: it not being intuitive is a problem--even if it is not classified as ambiguous.

It being only contextually intuitive, also seems somewhat problematic--but maybe excusable if it is assumed you know some similar language going into it and it behaves similarly to that other language.


No idea what GP was trying to say, but the C style intuition is correct. The bug in that code is due to omitted brackets, it has nothing to do with semicolons/ASI.


My point was that its hard to tell where the semicolon will be inserted. As well as the intention of the programmer. Here is another example:

    if(x)
    return
    "foo"

    return callback
    (1,2,3,4)


I think I see what you're saying, but the context here was asking for cases where omitting semicolons causes non-obvious bugs. I mean:

    if(x)
    return
    "foo"
clearly there's a bug there, but adding in semicolons wouldn't change the code's behavior. So it doesn't affect the "is it bad to omit semicolons?" debate either way.


    if(y) return
      "bar"
The linter will tell you there should be a semicolon, and you are like, - no it shouldn't.


For the benefit of the rest of us, can you explain what issues this causes and where/how you are running this code?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: