JavaScript if Statements Syntax

July 09, 2010

A very fundamental part of JavaScript is the if statement. One thing that gets people all up-tight is using ternary operators and not using curly brackets in your JavaScript. Some argue that it isn’t readable. In this article I’ll show you what practices I use and hopefully you can give me your thoughts.

Normal if statements

Nothing special here, just a normal if statement.

foo = "bar";
if ( foo == "bar" ) {
  foo = "something";
} else {
  foo = "somethingElse";
}

If statements can be written without curly brackets

foo = "bar";
if ( foo == "bar")
  foo = "something";
else
  foo = "somethingElse";

What people don’t like about this is if you need to do another action inside the if statement, then you have to manually go back and wrap it in curly brackets.

foo = "bar";
if ( foo == "bar" ) {
  foo = "something";
  doSomethingElse();
} else
 foo = "somethingElse";

The moral here is to not eliminate curly brackets if you are performing actions based on a conditions. Actions are likely to change and you will frustrate team mates and yourself with this.

Ternary operators

foo = "bar";
foo = foo == "bar" ? "something" : "somethingElse";

I pretty much only use ternary operators in the case that you are setting a value to a variable based on a condition. Sometimes I’ll indent it like this to make it more clear.

foo = isSomething ? 
  "something" : 
  "somethingElse";

I think this shortened syntax is great if your variable setting is conditional. I usually don’t use ternary operators if there is actions being performed, because those actions may be likely to change in the future and are a little pain to rewrite the logic around.

When eliminating curly brackets is probably ok

The only case that I don’t put in curly brackets is if there is one, single action that is unlikely to change.

if ( json.message ) $.jGrowl(message);

The reason I find this particular case ok is that we aren’t going to do anything else with the message. The application code is most likely to just run a function on the message and do nothing else with it.

What about you? How do you manage the shorthanded niceties that JavaScript is built with?

11 comments

#1. Jörn Zaefferer on July 09, 2010

I leave out the brackets only around return statements.

Also I prefer to format the ternary with the question mark and colon in front of the expressions, indented.

#2. TheDiLab on July 10, 2010

I use brackets all the time, except to assign a value to variable.

#3. Manmohanjit Singh on July 10, 2010

Great tip, have been using the normal if statements for a long time in both Javascript and PHP

#4. veged on July 10, 2010

json.message && $.jGrowl(message);
it’s shortest

#5. Benoit on July 12, 2010

I do exactly the same. The only difference is i’d just indent your last sample.

if ( json.message ) 
    $.jGrowl(message);
#6. Gilberto Ramos on July 12, 2010

I use brackets all the time too..! But I minify the file to use it in production!

  1. good one
#7. Matt on July 13, 2010

In C#, the common practice for the opening curly brace of an “if” statement is to have the curly brace begin on a new line.

Is that bad practice for javascript? (I think that might be discussed in one of Douglas Crockford’s videos…)

#8. Marc Grabanski on July 13, 2010

Matt, I don’t see any JavaScript developers in open source using open brackets starting on a new line.

#9. Matt on July 13, 2010

Yeah, I rarely see that either, but I’ve noticed when I use them, my code readability improves, and it’s easier to debug an error in a complicated “if” statement. If the style has no adverse effects on the code, I may keep trying to use it…

#10. koskoz on July 15, 2010

I only use ternary operator for conditions inside a html tag for instance.
I always write curly bracket since jslint throws me errors when I don’t :)

#11. Nick Tulett on July 26, 2010

I like the object literal construct in Rebecca Murphey’s “jQuery Fundamentals” ebook:

// old way
if (type == 'foo' || type == 'bar') { ... }

// better
if (/^(foo|bar)$/.test(type)) { ... }

// object literal lookup 
if (({ foo : 1, bar : 1 })[type]) { ... }

That last one is sick – and would thus make a great interview question.

Leave a comment

Comment in textile images by gravatar