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

I'd just like to add that Go has automatic semicolon insertion, and I have never experienced any surprises with it, thanks to gofmt.


Go doesn't have automatic semicolon insertion. It's just not required unless for splitting multiple commands on the same line. So falls into the same category as Bash in the GPs examples. Thus Go's syntax has been designed around having semicolons omitted.

Whereas JavaScript technically requires it but many browsers decided to support parsing missing semicolons for the sake of making bad websites render (much like how most browsers will support a variety of incorrectly formatted HTML documents as well). The problem here is that JavaScript's syntax wasn't designed to have semicolons omitted. Which leads to all sorts of edge case problems and no clear answer on how they should be handled.


The JS part of your comment isn't correct at all. ASI rules are fully described in the very first JS spec, and in 20+ years of JS coding I can't recall ever having heard of browsers handling it inconsistently.


Maybe it's the way the language is taught that has changed? Originally (pre-ECMA days) you were taught that you should always include a semi-colon. The older specifications certainly don't encourage dropping semicolons liberally either. Take this passage from the specification:

> 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.

Which, to me at least, reads as saying JavaScript must include a semicolon but interpreters must be tolerant of the occasional missing semicolon where intent is obvious.

Somewhere along the line the attitude seems to have changed from "you should include them" to "don't bother". Or at least that's perception I get these days.

I've not been heavily involved in JavaScript for a few years but I was one of the early adopters back in the days of Netscape Navigator and, later, Internet Explorer (albeit with it's differing but ever-so-similar "JSScript").

As an aside, since I'm talking about the early days of web scripting, wouldn't it be great if we brought back the language property of <script> to support other languages. Wouldn't be sensible in practice (for a whole plethora of reasons) but one can dream....


> Originally (pre-ECMA days)..

JS appeared in Dec 95 and the first ECMA spec came in June 97, so that's a pretty narrow window. But it's certainly true that ASI style (omitting semicolons) was relatively rare before, say, 2010-2014 or so. Its subsequent rise in popularity is probably thanks to Node (which uses ASI style), and a small group of ASI developers whose code suddenly got more visible once npm came along.

But as a practical matter, the rules on ASI have been the same since forever, and the rules are that they're nearly always optional. The only realistic case where omitting them causes issues is when you start a line of code with a parenthesis or a square bracket. Back when ASI first got popular that was the one big gotcha, but nowadays thanks to linters even that's not an issue.

What you may be remembering is, some years back there were some cases where certain JS tools didn't support ASI. Famously, there was a thing where a popular minimizer (JSMin) broke ASI code and Crockford didn't want to fix it because he didn't like ASI style (he later relented). But I've never heard of an actual JS engine not supporting ASI correctly.


> JS appeared in Dec 95 and the first ECMA spec came in June 97, so that's a pretty narrow window.

In theory yes, in practice not really. IE4 was the first version of JScript to be based on ECMA and that was released at the end of 1997. But in that era people didn't update regularly. Sure some folk (myself included) installed IE4 on top of Windows 95 SE[1]. But a lot of people wouldn't have had access to IE4 until they bought a new PC with Windows 98 pre-installed. So there was a good few years in practice.

Also worth noting that the web changed so much between 95 and 98. It might have only been three years in real terms but it felt like a lifetime in terms of technical revolutions.

[1] If I recall correctly, I had IE4 because it came on a .Net Magazine cover CD (not to be confused with Microsoft .NET: https://en.wikipedia.org/wiki/Net_(magazine) )


The second most popular formatter is semi-colon-less https://standardjs.com/ Also yes, strong agree the web should support other languages and it is very easy to achieve by integrating the polyglot and interoperable graalVM


I never have trouble with automatic semicolon insertion in real JS-based projects thanks to similar format tools. JS only really has this problem when it is serving its unique position as the scripting language of the web.




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

Search: