4 ms·
JavaScript’s “automatic semicolon insertion” rule
I had no idea about the below found at [1],
very interesting, I'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.
Ask HN: Is there any other language that does the same?
[1] Quote from http://www.craftinginterpreters.com/scanning.html :
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.
- JoelJacobson 10y agoSuggestion: 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 10y agoI 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 10y ago"use semicolons";
- bananicorn 10y agoYes, "use strict"; is already there for that - for anyone who is wondering: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Strict_mode https://developer.mozilla.org/de/docs/Web/JavaScript/Referen...
- aprdm 10y agoMost 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)