4 ms·
It still surprises me that this discussion pops up so often. Even more surprising is that people assume as a matter of fact that forgetting to put a semi-colon
by bilalq 11y ago
It still surprises me that this discussion pops up so often. Even more surprising is that people assume as a matter of fact that forgetting to put a semi-colon is the problem.
> When your code’s intention is ambiguous, your JavaScript engine will attempt to fill in the gaps.
This is kind of misleading. The JS engine isn't making some guess. There are clearly defined semantics on how ASI (automatic semi-colon insertion) operates. Let's put aside whether ASI is good or bad-it's just part of JavaScript. Ending all your lines with semi-colons won't save you, only understanding ASI will do that.
Take this example:
function foo() {
return
'A really long string that I've put on another line';
}
console.log(foo());
You'll see that `foo` returned undefined here. Why? Because ASI terminated `return\n` as a statement.
Isaac Schlueter wrote a post on this years ago that pretty much says everything I'd want to say on this topic:
http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding http://blog.izs.me/post/2353458699/an-open-letter-to-javascr...
- ifdefdebug 11y agoSo if I get you right, return "something"; is parsed as return; "something"; ??? That's just braindead.
- Tloewald 11y agoYes, but unfortunately you can't go back in time and fix it. What we really need is for an analog of "use strict" (e.g. "use strict semicolons" that either (a) eliminates semicolon insertion from Javascript (and just starts throwing errors) or (imho better) (b) eliminates the semicolon altogether (aside from inside for()). Since a huge proportion of production js is being linted using tools that force semicolon insertion, the first option would be pretty painless. Option b — making newline the semantic replacement for ';' would actually be better, since it prevents atrocities like: function wtf(x){ console.log('wtf', x); } /* newline here; strictly correct js */ ("hello world")
- musha68k 11y ago"Semicolons in JavaScript are optional" <3 http://mislav.net/2010/05/semicolons/ http://mislav.net/2010/05/semicolons/
- choward 11y agoWow, that is the biggest pile of garbage I have ever read. Half of this article is a strawman argument under the heading: "You can’t minify JavaScript code without semicolons" I've never heard that argument in my life for using semicolons. > Easy solution: when a line starts with parenthesis, prepend a semicolon to it. Sweet! Something extra to remember because I chose not to use semicolons. > My advice on JSLint: don’t use it. Why would you use it? If you believed that it helps you have less bugs in your code, here’s a newsflash; only people can detect and solve software bugs, not tools. So instead of tools, get more people to look at your code. Are you kidding me? This has to be trolling. Our goal as programmers is to automate tasks and save people time. What kind of garbage is this?
- bilalq 11y agoI'm firmly on the no-semicolon camp, but I agree with the general sentiment of your comment. >Half of this article is a strawman argument under the heading: "You can’t minify JavaScript code without semicolons" I've never heard that argument in my life for using semicolons. This does unfortunately still happen. Even in this HN discussion, it's been brought up. Nowadays, every minifier follows proper ASI semantics. Concatenation is something you need to be careful of, but that's more of a general thing that has nothing to do with whether or not semicolons are used. Joining files with ";\n" and wrapping contents in IIFEs work well. > Sweet! Something extra to remember because I chose not to use semicolons. A linter can catch this for you, so you don't actually have to remember anything. Regardless, it's a simple rule: Prepend a semi-colon to lines that start with '[' or '('. This is much simpler and less noisy than "Put a semi-colon in most places that terminate a statement". More importantly though, I'd say that ASI rules are something everyone should be aware of, regardless of whether or not one chooses to use semicolons. > Are you kidding me? This has to be trolling. Our goal as programmers is to automate tasks and save people time. What kind of garbage is this? Agreed. JSLint in particular is quite rigid, so I'd avoid using it, but ESLint is a fine alternative that can be configured to check for both semicolon and semicolon-free styles.
- timmyelliot 11y agoYour point is made (but, slightly off-topic: that single quote in "I've" would cause issues with your code).
- dugword 11y ago> Ending all your lines with semi-colons won't save you, only understanding ASI will do that. It will save you if you use JSLint/JSHint/ESLint/etc. In your example you will get an error about a semicolon missing after the return statement. If your convention is to always use semicolons your editor can let you know you have made a mistake. If you don't use semicolons then you have to turn off these warnings from your Linter and you won't catch the error until you see weird results at runtime. And that's the purposing of always using semicolons, so you can have automated tools help you.
- bilalq 11y agoSure, a linter can handle that situation. But linters can handle the semicolon-free style just as well[1]. There is no real value-add from semicolons; they're just a matter of stylistic preference. [1]: https://github.com/feross/standard https://github.com/feross/standard