TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Semicolons in JavaScript are optional

31 pointsby sailormoonalmost 15 years ago

18 comments

axodalmost 15 years ago
Use semicolons. Always. It's looks cleaner, and you won't come up against stupid errors like<p><pre><code> return { foo: 7 } </code></pre> From the article -<p><pre><code> When you're done trying to wrap your brain around why would anyone in their right mind want to write a return statement on a new line </code></pre> Well, if they love having { on newlines, then it's pretty obvious:<p><pre><code> function foo() { return { bar: 8 } } </code></pre> From the article -<p><pre><code> That's 24 bytes right there. Stamp semicolons everywhere and run it through a minifier: var a=1;var b=2;var c=3; </code></pre> Yeah um, hate to break it to you, but a minifier (and any experienced coder) would write it as "var a=1,b=2,c=3;"<p><pre><code> // after minification if(condition){stuff()} </code></pre> Wrong again. Use a better minifier. Closure advanced mode for example will remove the {}, inline functions if it makes sense and hundreds of other things.<p><pre><code> Easy solution: when a line starts with parenthesis, prepend a semicolon to it. ;(d + e).print() </code></pre> Ugly horrible hacky advice.<p>I remember when I first saw C code (coming from BASIC) and thought similar thoughts - eugh what are all those useless semicolons they don't do anything what's the point of them etc.<p>One important point is that it allows you to rearrange the whitespace in your code, without changing the execution meaning of your code. Which is pretty useful in making your code beautiful and readable and avoiding bugs.
评论 #1460321 未加载
评论 #1460469 未加载
评论 #1460312 未加载
travemalmost 15 years ago
Do we really want to heed advice when it's comes with obviously wrong advice like "Only people can detect and solve software bugs, not tools."<p>Static bug analysis is definitely not the be all and end all of software quality but definitely has a place. We automatically run FindBugs (along with unit test etc) on our code base every time new code is pushed by a developer and it definitely helps pick up quirky little errors much more cheaply than code reviews.
jameskiltonalmost 15 years ago
I use semicolons, and a linter to let me know when I miss them, because IE pukes all over itself if I don't. Same with trailing commas, IE HATES those.<p>Article ignores reality.
评论 #1460696 未加载
weegoalmost 15 years ago
text on there made my eyes hurt.<p>the basic message seems to be "don't do what everyone else is doing just because everyone else does it".<p>Which is great in your bedroom, but in a team with lots of eyes on your code the most efficient way is to use the same style, with that style being one that is easy to read and that avoids potential misunderstandings and bugs.<p>I can't imagine any situation in the sphere of development this article is about in which worrying about a couple of hundred bytes is more important than having a set of code that someone other than yourself could happily sit in front of.
评论 #1460641 未加载
nixyalmost 15 years ago
<p><pre><code> I write semicolon-less code and, in my experience, there isn't a JavaScript interpreter that can't handle it. </code></pre> Come visit me at work and I'll show you a few JS implementations on various set-top-boxes that will freak out if you don't use semicolons.
评论 #1460438 未加载
Tichyalmost 15 years ago
The minifier he uses seems rather bad, wonder which one it was? Ie instead of var a = 1;var b=1; it could be var a=1,b=1; Also in case of the single line if, the minifier could actually remove the brackets.
mike-cardwellalmost 15 years ago
His comments about minification are silly. It's entirely dependent on the minifier. Google Closure on my system converts:<p>if(condition) stuff()<p>To:<p>condition&#38;&#38;stuff()<p>So no, it doesn't add curly brackets and increase the size of the expression. And Google closure converts:<p>var a=1<p>var b=2<p>var c=3<p>To:<p>var a=1,b=2,c=3<p>Not what he said all minifiers do.
lzimmalmost 15 years ago
Umm, please let me restate jameskilton's position below (<a href="http://news.ycombinator.com/item?id=1460474" rel="nofollow">http://news.ycombinator.com/item?id=1460474</a>):<p>Use semicolons or your shit wont work in IE... That's why semicolons in javascript AREN'T optional.
subbualmost 15 years ago
What's wrong in using semicolons?
评论 #1460392 未加载
HendrikRalmost 15 years ago
Simple question: Which code does preserve your lifetime? Highly optimized, less readable but really short code, or highly readable, good structured, better maintainable code? As for me, I really enjoy sharing my code with my colleagues. Maybe there are others who don't, but they always will have to work alone in the basement where light never reaches theirs pale faces (think of 'The IT Crowd', for example).
abpalmost 15 years ago
Hm, if we look a little more generalized at what that says, its like:<p>"If theres a language/library feature with a few quirks, use it anyway, if the border cases aren't that usual."<p>Thats really bad advice. The main disadvantage is that no one will, if you combine a lot of such decisions, have all this border and special cases in mind, all the time.
earnubsalmost 15 years ago
FWIW If you're using JavaScript in PhotoShop the following produces an error because there's no semicolon after the second statement:<p><pre><code> for(i=10;i--) { /* do stuff */ } </code></pre> So maybe that's an example of a JS implementation where it differs on optional semicolons.
评论 #1460714 未加载
snitkoalmost 15 years ago
If you're so desperate about semicolons, maybe you should just use CoffeeScript instead?
TrevorBurnhamalmost 15 years ago
Gotta make a plug here for CoffeeScript, in which semicolons really are optional. CoffeeScript code compiles to neat, legible JavaScript (with semicolons).<p><a href="http://coffeescript.org" rel="nofollow">http://coffeescript.org</a>
RommeDeSerieuxalmost 15 years ago
I fail to see the problem with minification. The minificator can insert semicolons by itself because the algorithm for that is known and present in every JS engine. Besides, it just looks cleaner without them. Less for errors too.
phoboslabalmost 15 years ago
The way this semicolon-free syntax is implemented should be reason enough not to rely on it: if the interpreter/compiler encounters an error, it steps back, inserts a semicolon and tries again.
mawalmost 15 years ago
I bet this guy thinks using tabs in source code is a bright idea too.
seasoupalmost 15 years ago
wow, quite the rant about... um... semicolons. yeah, ok, I only skimmed. much more interesting things out there.