JavaScript Brace Alignment

Question
How should I align my braces when writing in JavaScript?

Answer
The most common recommendation is to place opening braces on the same line as the start of a code block.

Comments
If you place opening braces on the line following the start of a code block, you run the chance of introducing automatic semicolon insertion errors.

Examples

// Incorrect.
if (someCondition)
{
  doWork();
}
else
{
  doNothing();
}

// Correct.
if (someCondition) {
  doWork();
} else {
  doNothing();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s