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.

Poll: Semicolons in JavaScript

24 pointsby felixgealmost 15 years ago
Question: Should people always use semicolons in Javascript?<p>For those unfamiliar with the matter, Javascript allows semicolons to be omitted sometimes, see:<p>http://inimino.org/~inimino/blog/javascript_semicolons<p>Update: This poll was started as a result of a discussion on the node.js mailing list:<p>http://groups.google.com/group/nodejs/browse_thread/thread/35810f231cb289aa

19 comments

felixgealmost 15 years ago
Personally I think the Google Javascript Style Guide has it right:<p>&#62; Relying on implicit insertion can cause subtle, hard to debug problems. Don't do it. You're better than that.<p><a href="http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Semicolons#Semicolons" rel="nofollow">http://google-styleguide.googlecode.com/svn/trunk/javascript...</a>
评论 #1547930 未加载
评论 #1547811 未加载
评论 #1549397 未加载
jasonkesteralmost 15 years ago
Readability is the single most important quality of a piece of code. Semicolons aid readability by eliminating ambiguity and ensuring that nobody ever has to spend even a second figuring out whether your code will do what it looks like it will do.<p>I add semicolons for the same reason I surround <i>if</i> blocks with braces, pad out blocks with whitespace, and avoid the ternary operator in all but the most trivial cases. If you ever find yourself dropped onto a line of my code by a debugger, chances are you'll be able to figure out exactly what's going on immediately.<p>At least that's the goal.<p>(more here: <a href="http://expatsoftware.com/articles/2007/06/getting-your-priorities-straight.html" rel="nofollow">http://expatsoftware.com/articles/2007/06/getting-your-prior...</a> )
评论 #1548479 未加载
tszmingalmost 15 years ago
Quote from Douglas Crockford (<a href="http://ajaxian.com/archives/book-review-%E2%80%9Cjavascript-the-good-parts%E2%80%9D-by-crockford" rel="nofollow">http://ajaxian.com/archives/book-review-%E2%80%9Cjavascript-...</a>):<p>When I was a young journeyman programmer, I would learn about every feature of the languages I was using, and I would attempt to use all of those features when I wrote. <i>I suppose it was a way of showing off</i> ...<p>Eventually I figured out that some of those features were more trouble than they were worth. Some of them were poorly specified, and so were more likely to cause portability problems. Some resulted in code that was difficult to read or modify. Some induced me to write in a manner that was too tricky and error-prone. And some of those features were design errors. Sometimes language designers make mistakes.<p>Most programming languages contain good parts and bad parts. I discovered that I could be a better programmer by using only the good parts and avoiding the bad parts. After all, how can you build something good out of bad parts?
jacquesmalmost 15 years ago
For me it's a 'yes' based on the simple rule that explicit is always better than implicit. That sort of 'optimisation' seems to be really cool when you're just starting out in a language but in the long run it isn't worth it.<p>The first time you're bitten in the behind by a subtle bug after a couple of hours searching you've given up a hundredfold the time you have saved up to that point by not having to type those ;s. At that point people usually 'get it'.
评论 #1547892 未加载
troelsalmost 15 years ago
When the curly-braces-syntax is replaced with proper sexp, I'm all for it. Until then, I prefer to keep the semicolons.
评论 #1547828 未加载
wlievensalmost 15 years ago
Yes, because JSLint tells me to do so, among other reasons.
lhoriealmost 15 years ago
fwiw, I often see this<p><pre><code> var bla = function() { //stuff //more stuff //some more }//no semicolon</code></pre>
mumrahalmost 15 years ago
Absolutely. Also, always use "var" in your variable declarations. I can't count how many times a scope issue bit me in the ass b/c of a missing "var"
评论 #1549496 未加载
technoweeniealmost 15 years ago
Client side - yes. Server side - no. Server side code typically only runs through one interpreter, and is never minimized.
Terhorstalmost 15 years ago
JS minimizers tend to remove all the newlines, so semicolons are often necessary if you use one.
评论 #1548105 未加载
评论 #1547829 未加载
x5315almost 15 years ago
The case for semicolons within javascript is one that you will hear a lot. You'll hear many cries for 'readability', 'eliminating random bugs' and 'helping minification'.<p>To begin with readability, i'm pretty sure that programmers are capable of working out the end of a line without the use of a semicolon: Scala has no such requirement of semicolons. Surely, the best way to ensure readability is, like in spoken language, to use common terms and expressions. Finding the obscure uses of the language to ensure the forceful requirement of a semicolon just seems petty and unhelpful. It is the equivalent of being overly prolix within a natural language, therefore limiting the ability to communicate.<p>The elimination of random bugs is a little more tricky to discern. Sure, there are occasional issues that can be caused by the lack of semicolons, for example (taken from the quoted text):<p>a = b + c (d + e).print()<p>But let's face it, if you're writing your javascript like that then you probably need to re-evaluate the your use of (d + e). After all, the majority of browsers now use javascript compilers that are more than capable of constant folding. Therefore meaning that:<p>a = b + c var f = d + e f.print()<p>should therefore be no less efficient and, on the side of readability, you could even name f to be something worthwhile.<p>The best minifiers (YUI and Google's Closure Compiler) are perfectly capable of minifying javascript without semicolons. If your minifier is having issues because you haven't inserted a semicolon, then—obviously—it's not doing its job properly.<p>In my writing of javascript, I very rarely use semicolons and the issues described have never come up. Often the expressions given as to why it is necessary are extreme and obscure to the point whereby using those expressions may not be the best policy. I'm not advocating the idea of never using semicolons (something i pretty much do), I'm just saying that the requirement for constant use is pointless.
domnitalmost 15 years ago
It seems like semicolon insertion is an example of browsers being liberal acceptors of slop. Good that the rules are codified by ECMA, much like HTML5's error recovery parsing rules, but I wouldn't omit semicolons on purpose.
hcmalmost 15 years ago
Let Crockford himself explain why 'Yes' is the right answer: <a href="http://www.youtube.com/watch?v=hQVTIJBZook#t=30m39s" rel="nofollow">http://www.youtube.com/watch?v=hQVTIJBZook#t=30m39s</a>
IsaacSchlueteralmost 15 years ago
My comment on the subject:<p><a href="http://groups.google.com/group/nodejs/msg/de4630fa8953b183" rel="nofollow">http://groups.google.com/group/nodejs/msg/de4630fa8953b183</a>
noodlealmost 15 years ago
yes, because the consistency removes it as a potential cause of bugs, and makes my code easier to debug by someone else.
esschulalmost 15 years ago
Never make the browser guess.
emehrkayalmost 15 years ago
Just do what Doug Crockford says, that definitely includes braces
polotekalmost 15 years ago
I respectfully abstain from the vote.
SubtleGradientalmost 15 years ago
Mind your own business!<p>It's none of your business how I choose to code my own projects.<p>If we're on the same project together, we can discuss it and decide on a style that works for everyone.<p>When I contribute to an OpenSource project, I adopt whatever style they have defined.<p>For my own projects, I have personally opted out of using unnecessary semicolons and brackets unless they improve maintainability or readability. But you don't have to know or care about that.
评论 #1550530 未加载