TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Why use semicolons to terminate statements in modern JavaScript or ActionScript?

13 点作者 timknauf超过 16 年前
I hope the question doesn't come across as flamebait, because I'm genuinely interested in the response here. My background is mainly in the largely semicolon-less Python and Ruby. This may have impeded my understanding of the issues involved. :^)<p>ECMAScript-derived languages have inherited many syntactical conventions of C-style languages (e.g. braces to demarcate blocks). However, while semicolons are required to terminate a statement in C-style languages like Java, they are optional in modern variants of Javascript and Actionscript, where a line feed is considered to imply a semicolon if the statement on that line is complete.<p>Being able to leave out semicolons is blissful as far as I'm concerned. When coding in Java, I frequently run into compiler errors because I have neglected to type a semicolon, whereas I can't think of a single time when omitting semicolons in Javascript or Actionscript has caused me problems.<p>However, the code style of others (e.g. website Javascript, or Actionscript open source projects) suggests that my opinion mustn't be widely held; I see semicolons everywhere.<p>Now, Javascript on a web site has to run in many different browser environments, and I can understand how fear of incompatibility could keep coders locked to semicolons (although like I say, I haven't seen any browser problems myself). But Actionscript gets compiled, removing the uncertainty about how syntax will be interpreted on the client. So why are semicolons still being used now that they are optional in Actionscript 3.0? Do semicolons provide a secret benefit that I'm not aware of?

8 条评论

shutter超过 16 年前
Two areas that I can come up with:<p>1. Minification -- If you want to minify your JS, you might run into problems if you don't insert all necessary semicolons.<p>2. Convention -- If you wrote js code with no semicolons and then showed it to other JS programmers, you'll get a funny look, a slap in the face, or a free copy of "Javascript for Dummies", depending on the context.
评论 #374833 未加载
评论 #374554 未加载
axod超过 16 年前
Leaving out semicolons leads to errors. Ambiguity etc...<p>return<p>[1,2,3]<p>What should that do? Should it return, or return [1,2,3]? There are many other examples. Use semicolons and there is no debate over what should happen. Making them optional in javascript was a ridiculous idea.
评论 #374655 未加载
评论 #375728 未加载
评论 #374637 未加载
评论 #374758 未加载
timknauf超过 16 年前
Okay, putting all talk of practicality (interpreters, minifiers, convention) aside, let's look at the language itself for a bit; this could lead to some interesting discussion.<p>JavaScript has two ways of designating the end of a statement. Here is (1):<p><pre><code> ;\n </code></pre> And here is (2):<p><pre><code> \n </code></pre> Now, the semicolon can't help you if you type:<p><pre><code> return\n {a:1\n b:2};\n </code></pre> because statement end (2) has already kicked in, and so the code will return undefined.<p>So, given that statement end (2) <i>is</i> part of the standard (and doesn't look like leaving it anytime soon), is there any <i>language</i> reason to use statement end (1)? Is it purely a visual signifier? If so, is that useful to a majority of coders? Am I in a minority for considering it visual noise? Remember, we're putting aside practicalities like existing conventions, minifier capabilities and browser implementations for the sake of the exercise.
sh1mmer超过 16 年前
Minification has already been mentioned, but I wanted to re-iterate as it's such an important point. You can reduce size over the wire by as much as 30-80% by minifying and obfuscating (something like YUI or Dojo compressors do this).<p>Not having semi-colons risks a lot of code breakage. Doug Crockford's JSMin has an aggressive mode which always strips line breaks and another which doesn't to avoid breakage when semicolons don't exist.<p>Personally I would just say that if you don't use semi-colons you should think hard about why you code at all. I'm not the anal-retentive type but sloppy code isn't awesome. I would put this in the same category as naming variables $whatever or $x. If you've never picked up someones second hand project you may not appreciate why a pleasant coding style matters.
评论 #374586 未加载
bdfh42超过 16 年前
Explicit statement termination is all about speed - ensuring that JavaScript interpretation is as rapid as possible. Terminating a statement implicitly requires additional processor cycles - and we still need to husband those cycles to maintain a responsive user experience.
评论 #374643 未加载
评论 #374660 未加载
timknauf超过 16 年前
Thanks for the responses, everyone! Sounds like - regardless of what I might think of semicolons personally - the reality of the current crop of minifiers and JavaScript engines makes a case for their continued use.<p>Now, how about ActionScript? This has a compilation step, so the Flash VM is not dealing with your code directly. If we discount ActionScript obfuscators, could a stronger case be made for omitting semicolons there?
danw超过 16 年前
I highly recommend you use JSlint to pick up on things such as forgotten semi-colons to improve the quality of your code.
illumen超过 16 年前
they allow copy/paste with other C syntaxish languages<p>edit: Sorry, I forgot to include caps, and full stops for that first sentance.
评论 #374556 未加载