Difference between revisions of "ProgClub standard JavaScript format"

From ProgClub
Jump to: navigation, search
(White space...)
(Work, work...)
Line 9: Line 9:
 
  if ( true ) return foo();
 
  if ( true ) return foo();
  
All on one line if only one statement:
+
All on one line if only one short statement:
  
 
  if ( true ) { return false; }
 
  if ( true ) { return false; }
Line 21: Line 21:
 
  }
 
  }
  
Multiple lines if and if-block is followed by else/else-if:
+
Multiple lines if an if-block is followed by else/else-if:
  
 
  if ( true ) {
 
  if ( true ) {
Line 38: Line 38:
 
  if ( true ) { return false; }
 
  if ( true ) { return false; }
 
  else { return true; }
 
  else { return true; }
 +
 +
Open curly bracket on same line, close curly bracket on new line:
 +
 +
function test() {
 +
 +
  return true;
 +
 +
}
 +
 +
Always use a semicolon if you can!

Revision as of 00:02, 12 April 2016

OK, so if there's such a thing as the ProgClub standard JavaScript format I'd better explain what that is... this page remains a work in progress.

Always use curly brackets even when not required:

if ( true ) { return foo(); }

// not:

if ( true ) return foo();

All on one line if only one short statement:

if ( true ) { return false; }

// not:

if ( true ) {

  return false;

}

Multiple lines if an if-block is followed by else/else-if:

if ( true ) {

  return false;

}
else {

  return true;

}

// not:

if ( true ) { return false; }
else { return true; }

Open curly bracket on same line, close curly bracket on new line:

function test() {

  return true;

}

Always use a semicolon if you can!