1. 12

  2. 8

    tl;dr: Always use semicolons

    1. 9

      This is the worst trend in modern JS IMO. There’s no reason not to use semi-colons in this language.

      1. 4

        A E S T H E T I C

      2. 7

        And always use brackets around statement blocks. I have no idea why so many people think that they are being clever by eliminating basic syntax.

        1. 2

          “It’s fewer characters therefore it’s simple and lightweight” - the same reason I see justifying React hooks. You still have an object, you still have a stand-in for “this”, but now it’s secret and hidden from the application programmer in ambient library magic, and therefore your code is somehow “functional” despite being full of mutable state.

          1. 1

            because it feels alright to omit them when you’re writing the statements for the first time because the context is fresh in your memory

        2. 3

          If you liked that check out what this snippet of code prints:

          n = 1
          /1*"\/\//.test(n + '"//')

          The explanation is left as an exercise for the reader.

          1. 2

            It wasn’t hard to figure out, though I’m usually the one doing the regexes around here. Also, being familiar with Javascript concatenation rules helps!

          2. 3

            The problem with these solutions is they don’t suppose that code files are concatenated. Semicolons on statements beginning with the characters [ or ( should always start with a semicolon in this case.

            1. 3

              If you’re concatenating code files it seems like a really bad idea not to put semicolons between the files, for exactly this reason. (If you do this step before minification you don’t even need to worry about adding an extra ten bytes or whatever.) Mashing two files together and assuming that the aggregate will behave the same way doesn’t work in most programming languages—and in general, it doesn’t work in JavaScript either. If you’re going to manipulate code as text like that you need to take the syntax into account.

              1. 2

                It does work in javascript if you follow the rules for doing so.

            2. 3

              This debate will never end. For good measure, some arguments against semicolons.

              1. 4

                I’m sorry, but that article is almost comedy. The author suggests to work around problems that the lack of statement terminating semicolons causes, one should prepend a semicolon to lines that begin with a parenthesis, bracket, or arithmetic operator.

                ;(d + e).print()

                What?!? is that passing code review anywhere? Sometimes doing something because everybody else is doing it is in fact a valid reason.

                1. 1

                  Don’t be sorry, if you need semicolons just put them. In my limited Javascript experience, they’re useless unless you’re writing some of the edge-case mentioned in the original post. Not an issue for me, so far. ¯_(ツ)_/¯

                  Fun test: remove all ; from your current codebase, and see if that breaks any test.