Semi colons;
Jochem Stoel

Jochem Stoel @jochemstoel

About: Pellentesque nec neque ex. Aliquam at quam vitae lacus convallis pulvinar. Mauris vitae ullamcorper lacus. Cras nisi dui, faucibus non dolor quis, volutpat euismod massa. Donec et pulvinar erat.

Location:
Inner Earth
Joined:
Sep 30, 2017

Semi colons;

Publish Date: Nov 18 '18
10 13

A close friend and I got into an argument a couple days ago because he wholeheartedly believes (ECMAScript) code without semi colons equals bad practice for reasons I understand completely but don't agree with. (we fight about important things like this all the time)

I personally write JavaScript without semi colons and am are very opinionated about it. The same goes for more people here on devtoo. I found that for instance moji has none either.

There is a wide abundance of discussions and people arguing about it on the internet so.

Where exactly in the ECMAScript specification does it say that semi colons are omitted/required or not?

Comments 13 total

  • Curtis Fenner
    Curtis FennerNov 18, 2018

    JavaScript explicitly has rules for automatically inserting semicolons. Hence, they aren't necessary.

    However, the rules aren't very good, in that they are likely to do the wrong thing.

    For example, consider the following:

    return
        firstLongExpression(thatTakesManyArguments, andPushesMoreToTheNextLine)
    

    It doesn't do what you mean, because the semicolon insertion rules say to put a semicolon here:

    
    return;
    firstLongExpression(thatTakesManyArguments, andPushesMoreToTheNextLine);
    

    Your expression's result is lost!

    If you set your IDE up to automatically insert semicolons / make your CI tools reject code that would have semicolons inserted automatically, you can avoid these mistakes.

  • Andres R
    Andres RNov 18, 2018

    I think its fine to code without, but like the steering committees are moving towards, we are not sure where the language will be in 2-20 years. So the issue is, do you want your code to survive intact without modification (bad idea, but a very slim use case) in 10 years? Then you probably want to use semicolons. At the least have an IDE or tool push them in.

    I believe their guidance is that they want to reduce clutter and what they call 'syntactic sugar' so they recommend not using them, although I think semi-colons actually increase readability. Especially when it's not your own code and you are reviewing it.

  • Saurabh Sharma
    Saurabh SharmaNov 19, 2018

    I just let prettier insert semicolons for me

  • JavaScript Joel
    JavaScript JoelNov 19, 2018

    Here's the deal. Pick semi or no semi. Use a linter and format on save. Stop worrying about it.

    One I started formatting on save, I stopped caring as much.

    Just add long as your code is consistent across the whole project, that is already a win.

    • Dávid Szabó
      Dávid SzabóNov 23, 2018

      This is the correct answer. Linter will take care of the edge cases and warning you if something is wrong.

  • Fabian Holzer
    Fabian HolzerNov 19, 2018

    Where exactly in the ECMAScript specification does it say that semi colons are omitted/required or not?

    Here:
    ECMAScript 2019 Language Specification, Section 11.9.

    Most ECMAScript statements and declarations must be terminated with a semicolon. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

  • Alex Lohr
    Alex LohrNov 19, 2018

    While you can write JS code without semicolons, you'll run into cases where you'll have to replace them with something different that is easily forgotten, like the following bug:

    console.log('the next line will produce an error)
    (function() { console.log('this is a normal IEFE') })()
    

    each line by itself will work, but together, they will fail, because the IEFE will be interpreted to be an argument to the function output of the first line. To fix that, you'd have to put a ! in front of the IEFE or a semicolon at the end of the first line.

    Since it is easier to forget the first than to remember the second - at least if you put a semicolon at the end of each statement, you'll see why this is a best practice.

  • Ben Halpern
    Ben HalpernNov 19, 2018

    I feel a little tripped up coding in Swift, which doesn't require have semicolons but really looks like it should.

  • Jochem Stoel
    Jochem StoelNov 19, 2018

    As much as I agree with you, I also enjoy the debate.

  • Chad Windham
    Chad WindhamNov 19, 2018

    #ShamelessPlug

    When I read current ECMAScript specs (1.5ish years ago) it clearly states semicolons are optional... That is because of automatic insertion when it is parsed out. People will point to very contrived situations where not having a semicolon causes an error, but they are few and far between. Also, there are lots of situations where using a semicolon doesn't do what you think it will and there is still a parsing error (also usually very contrived examples...). In the situations where it will throw an error just understand the rules and put in a semicolon, not hard once you understand. But the idea the "just using semicolons at the end of every statement" just magically makes everything "safer" is more rooted from years ago when some parsers didn't honor the language specs properly, but they do now. There is a reason semicolon free JS is becoming more and more prevalent.

    Because it is fine and within the specs of the language.

    But if code sacrificed to semicolons causes your brother to stumble, do the loving thing and throw in some semicolons...

  • Chad Windham
    Chad WindhamNov 19, 2018

    The problem is the both sides of the argument are correct. You can't convince devs who abide by them to not use them, because they are right. You can't convince devs who believe you can omit them they are wrong, because they aren't!

Add comment