Are Semicolons Necessary in JavaScript?




Should you use semicolons when writing JavaScript? Explores why omitting semicolons can be bad and why I have chosen to not use semicolons when teaching JavaScript.

Original source


27 responses to “Are Semicolons Necessary in JavaScript?”

  1. When you first start learning Javascript everything is hard. Teach people where, and why, to put in the semicolons and they will become better coders. I like putting in the semicolons. It's nice to see where something ends.

  2. I think semicolons should be used all the time because if you’re writing in a different language that requires semicolons (such as php) it makes the transition much easier. I write in php for the API and ES6 for the front end. Php requires semicolons. And as a general mentality I find it’s always better to be explicit.

  3. You don't need them. Install ESLint and set "semi" to "never" in your config. Enjoy writing code without semicolons which always works properly, if you just pay attention to ESLint.

  4. The reason I use semicolons in JS is the same reason I use them in human language. So what if we stop using punctuations in English. if we have no punctuations then how do we know when there is a pause or when a sentence is over its not about the computer reading the code its about the human reading the code

  5. I don't claim to be a fan of semicolons, and don't claim to be an expert by any means, but we have to recognize that, if the engine puts semicolons for you, that objectively means semicolons are part of the language. Also, "line starts with [, ( or ` ", in which you have to put an awkward semicolon in there, is a stylistic exception way more confusing than "it's an object property definition", where you have to not-so-awkwardly avoid a semicolon.

    I mean, it's no big deal to omit semicolons if you know what you're doing, but if there is a "superior" standard, I'd vote for semicolons all the way.

  6. I have always been a semicolin user… then i saw a few arguements about why you should avoid them and then i wrote a bunch of js with minimul usage of them (only in for loops which i probably could have used a forEach instead) and i gotta say. i do like writing js better without it 🙂 the only time i've had to use the semicolins is to prepend my [a,b,c…].forEach(…) and (function(){})() with it 🙂

  7. Well, I don't really care whether you use ; in code or not, but the reasoning is weird.

    Instead of Voodoo-coding, you could teach your students the difference between a statement and an expression, which would make it perfectly obvious where to put a semicolon (at the end of each non-block statement – in case you were wondering)

    Even more so, it would easily explain the border-cases you are describing in your video.

  8. After 15 years of programming in pascal, C, C++, mostly Java, Perl, some custom languages… all using semicolons, I got very used to them. Languages without, such as Python, seemed odd to me for it's lack of semicolons – an elegant language, but oddly loose-looking with the whitespace, partly because of the missing semicolons, but also for white-space code blocks (which are actually dangerous IMHO). Recently, when I came around to look at javascript without semicolons (browsing through npm code), I decided to try no-semis for a while. I was very uncomfortable with it at first. But after some time reading and writing lots of semicolon-free code, I adjusted. example here:

    https://github.com/quicbit-js/qb-json-tok/blob/master/test.js

    Now I admit that javascript without semicolons looks better to me and is easier to read than semicolon code. I just don't type them anymore. Like Kyle says, if you type two semicolons on every line, then you will understand how single semicolons feel to me now – but I'm happy to type them in code that already uses, just don't see any point in them anymore.

    Now if I could just get my eyes to adjust to 2-space indentation of feross/standard. Still working on that… but I think micro indenting really does affect my ability to recognize blocks quickly. Anyway – try without semis for long enough and you too may not want them back.

  9. Never trust a person, who says 'eXpecially' and does not use semicolons in JS.
    Yes, I'm aware that this sounds like an argumentum ad hominem fallacy, but I'm just joking here and making a point of completely disagreeing with the opinions expressed in the video.

  10. One more case you have omitted are ternary operators:

    var o = {}
    (true === true) ? console.log('a') : console.log('b')

    I think adding too many semi-colons is a much safer strategy than omitting them and trying to memorize all the edge cases, especially when you're a beginner.

  11. 0:22 Total falsehood.
    Semi-colons ARE required. If you don't put them in yourself, the JS engine inserts them for you. Sometimes in places that you didn't intend.
    And I have a funny feeling that most proponents of sans-semi-colon, don't really understand where they go.

    Don't be lazy. Learn the syntax.

  12. Really too bad Standard JS is promoting this contentious style. The standard promotes many very good points, but this one is just so obviously contentious, look at how much justification is needed. It's unfortunate because it's a deal breaker for some people, myself included. And then you miss out on the other many good style points in the standard. Bye bye Standard JS, too bad we didn't get to know each other better.

  13. Interesting point about education. I created the JS style guide at my last job, and my decision to forbid semicolons was driven by the Java developers' experiences writing JavaScript. To help them better understand that JavaScript was very different, I wanted the two languages to look as different as possible.

    …And I wanted to do fewer pinky-pushups.

  14. I understand the point of this video is primarily for JavaScript developers, but coming from a PHP background I can see pitfalls in the message when I think of multi-language developers. I find that if you learn the "proper way" of using semi-colons to clearly differentiate instructions in your code then that's just one more thing you don't need to relearn when moving to another language.

    I believe programming languages that share common principles will always be better than being completely different in the hopes that the adoption of them from other languages could be easier for students in the future.

Leave a Reply