Spacing for Block Statements

Question
What are some JavaScript style recommendations for spacing in block statements?

Answer
The Dojo Style Guide recommends no spaces between the keyword, opening parenthesis, and opening brace. The Google JavaScript Style Guide specifies one space before the opening parenthesis and one space after the closing parenthesis. The jQuery Core Style Guide goes one step further and recommends a space after the opening parenthesis and one before the closing parenthesis.

Comments
My personal preference is the one prescribed in the Google JavaScript Style Guide.

Examples

// Dojo Style Guide recommendation.
if(someCondition){
  doWork();
}

// Google JavaScript Style Guide recommendation.
if (someCondition) {
  doWork();
}

// jQuery Core Style Guide recommendation.
if ( someCondition ) {
  doWork();
}

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