
JavaScript’s “automatic semicolon insertion” rule - JoelJacobson
I had no idea about the below found at [1],
very interesting, I&#x27;ve always wondered why JavaScript is so much worse than other languages,
and this finally explains what they got wrong that all other languages got right.<p>Ask HN: Is there any other language that does the same?<p>[1] Quote from http:&#x2F;&#x2F;www.craftinginterpreters.com&#x2F;scanning.html :<p>JavaScript’s “automatic semicolon insertion” rule is the odd one. Where other languages assume most newlines are meaningful and only a few should be ignored in multi-line statements, JS assumes the opposite. It treats all of your newlines as meaningless whitespace unless it encounters a parse error. If it does, it goes back and tries turning the previous newline into a semicolon to get something grammatically valid.
======
aprdm
Most of people (citation needed) use some sort of frontend building pipeline
(gulp, bower, npm, what have you...) and usually they put something like
jshint which would fail a build if a semi colon was missing.

Certainly not elegant but it works (speaking as a python developer doing some
javascript stuff in react / angular)

------
JoelJacobson
Suggestion: Change this behaviour in the next version of JavaScript to _not_
automatically "turning the previous newline into a semicolon", but instead
throw a parser error to the developer. Perhaps there is a way to enforce this
in modern browsers? I don't know anything about JavaScript to be honest.

~~~
neilsimp1
I think the reason that it's still left in is for backwards compatibility. You
wouldn't want a browser to update this and instantly break a bunch of older
sites.

~~~
ksherlock
"use semicolons";

~~~
bananicorn
Yes, "use strict"; is already there for that - for anyone who is wondering:

[https://developer.mozilla.org/de/docs/Web/JavaScript/Referen...](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Strict_mode)

