最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - if, for, or while with an empty body, use empty braces instead of a semi-colon - Stack Overflow

programmeradmin8浏览0评论

Using google closure piler I get the following warning message:

WARNING - If this if/for/while really shouldnt have a body, use {}.

Here's what the documentation says:

This warning means that you have a semi-colon immediately following an if, for, or while statement. For example:

// Produces JSC_SUSPICIOUS_SEMICOLON warning:
if (true);
else alert('no');

Is this just a code convention thing or is {} better than ; for some reason?

Using google closure piler I get the following warning message:

WARNING - If this if/for/while really shouldnt have a body, use {}.

Here's what the documentation says:

This warning means that you have a semi-colon immediately following an if, for, or while statement. For example:

// Produces JSC_SUSPICIOUS_SEMICOLON warning:
if (true);
else alert('no');

Is this just a code convention thing or is {} better than ; for some reason?

Share Improve this question edited Jul 22, 2013 at 19:35 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Jul 22, 2013 at 15:15 tim petersontim peterson 24.4k63 gold badges185 silver badges303 bronze badges 4
  • Why wouldn't you just test for the opposite? – Evan Davis Commented Jul 22, 2013 at 15:17
  • @Mathletics what do you mean? – tim peterson Commented Jul 22, 2013 at 15:18
  • 2 It's more clear to me that the if/for/while has an empty body if I read {} – Ye Liu Commented Jul 22, 2013 at 15:18
  • 3 Why wouldn't you write if (!condition) alert('no') instead of the empty body? – Evan Davis Commented Jul 22, 2013 at 15:20
Add a ment  | 

1 Answer 1

Reset to default 9

This guards against things like:

if(condition);
{
    // oops, I really meant for this to be conditional.
    // now it always executes, regardless of condition.
}

The error raised forces you to explicitly put the empty {} before the ; so it's clear that that's what you intended.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论