45

Possible Duplicate:
Are curly braces necessary in one line statements in JavaScript?

I am almost positive of this, but I want to make sure to avoid faulty code. In JavaScript do single if statements need curly braces?

if(foo)
    bar;

Is this OK?

Community
  • 1
  • 1
rubixibuc
  • 7,111
  • 18
  • 59
  • 98

4 Answers4

58

Yes, it works, but only up to a single line just after an 'if' or 'else' statement. If multiple lines are required to be used then curly braces are necessary.

The following will work

if(foo)
   Dance with me;
else
   Sing with me;

The following will NOT work the way you want it to work.

if(foo)
   Dance with me;
   Sing with me;
else
   Sing with me;
   You don't know anything;

But if the above is corrected as in the below given way, then it works for you:

if(foo){
   Dance with me;
   Sing with me;
}else{
   Sing with me;
   You don't know anything; 
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
OM The Eternity
  • 15,694
  • 44
  • 120
  • 182
  • So in the first example, if I want to add a line to the `if` statement, my options are 1) break the code or 2) go back to the "less pretty" way, and in the process create an ugly diff by modifying multiple lines when I just want to add one. It seems like using curlies to start with is the most sensible option. – Nathan Long Jun 22 '17 at 15:14
31

While it's syntactically okay to omit them, you shouldn't. The one case where ambiguity strikes hard is

if (false)
    if (true) foo();
else
    bar();

This will run neither foo nor bar since the else belongs to the second if statement. No problem if braces are used:

if (false) {
    if (true) { foo(); }
} else {
    bar();
}
user123444555621
  • 148,182
  • 27
  • 114
  • 126
  • 8
    I think the ambiguity problem is overstated, especially when it's used as an argument against omitting braces. When writing a single statement/expression/syntactical "compound" such as `if (...)` per line (which one should do anyway, following the argument of code readability), it's never going to be a problem, because one would automatically use braces for multiple lines. – TheOperator Jan 29 '16 at 19:43
  • 4
    Plus 1 for the double if example, got hit with this one today. – rob Oct 28 '16 at 14:43
  • 1
    This is a famous parser problem called the ["dangling else"](https://en.wikipedia.org/wiki/Dangling_else). – Clint Pachl Mar 07 '18 at 20:58
  • I agree with the issue above but disagree with the solution... If you always only have the conditions on one line i.e foo() is always on a different line, and then you adopt a style where any if or else that has more than one line gets {},s it's very easy code. I always hated code which strung more than one thing together, the if(true) foo(); would always be on separate lines for me. In the if/else case, the first if would have curly's, the second if would not and the else would not. just my 2 cents... – David Bollman Apr 12 '19 at 16:27
  • I fully agree with TheOperator here. – Steve Jun 17 '22 at 14:49
7

Yes it is allowed. It is also discussed before:

But it should be avoided:

Community
  • 1
  • 1
Naveed
  • 41,517
  • 32
  • 98
  • 131
4

Yes, it's syntactically valid. But it is considered bad style.

If you wrote it on a single line, you could argue that there are situations where it's okay, because it is unambiguous.

 if (foo) bar;

In most cases though, using curly brackets adds to code clarity, which is a good thing. Code is more often read than written, and it should be as unambiguous as possible.

Also, if you at some point need to add a second statement, you will most definitely need curlies anyway.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • how is it ambiguous on 2 lines? I think it would be more confusing on one line with a separate statement right underneath. The grammar is the word `if` followed by `(` followed by an `expression` followed by `)` followed by a `statement`, which `{ statements };` is a statement, aswell as `bar;` in your example. It is unambiguous, you just need to know what a statement is... – grinch Apr 06 '13 at 06:07
  • 1
    @grinch That's circular reasoning. Nothing in the world is ever ambiguous as long as you know exactly and instantly what it is. – Tomalak Apr 06 '13 at 08:51
  • What i mean is that this is defined in the language grammar, so how could it be ambiguous? Not knowing the language construct that makes up an if statement does not make it ambiguous. – grinch Apr 07 '13 at 17:46
  • Of course it's not ambiguous to a lexer. The point is that humans are no lexers. Besides, this discussion has been gone through a thousand times, this is not the place to do it again. There is a reason that every static analysis tool for JS marks missing curlies as an error. – Tomalak Apr 07 '13 at 18:04