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?

asked Aug 19, 2011 at 6:43
0

4 Answers 4

61

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
31.3k22 gold badges110 silver badges134 bronze badges
answered Aug 19, 2011 at 6:49
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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();
}
answered Aug 19, 2011 at 7:02

5 Comments

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.
Plus 1 for the double if example, got hit with this one today.
This is a famous parser problem called the "dangling else".
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...
I fully agree with TheOperator here.
7

Yes it is allowed. It is also discussed before:

But it should be avoided:

answered Aug 19, 2011 at 6:46

Comments

5

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
31.3k22 gold badges110 silver badges134 bronze badges
answered Aug 19, 2011 at 6:44

4 Comments

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 That's circular reasoning. Nothing in the world is ever ambiguous as long as you know exactly and instantly what it is.
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.