Skip to content

JavaScript Style Guide

Anthony Super edited this page Jan 24, 2015 · 12 revisions

Formatting

80-character width lines. Newline at the end of each file. Use 2 spaces to indent.

Brace formatting should be on the next line for a named function and on the same line for a closure.

var doThing =  function()
{
  var x = function(arg){
    attack(x);
  };
}

Braces also go on the same line for if statements.

if(test === 10){
  return true;
}
else{
  return false;
}

Use /* comment */ for multi-line comments. Use // otherwise.

Use semicolons. When working with callbacks, put the brace on the same line as the paren:

function setCallback(obj)
{
  obj.onComplete(function(data){
    send(data);
  });
}

We use semicolons everywhere.

E V E R Y W H E R E.

Never rely on the implicit semicolon insertion.

Conventions, constructs, and naming

Never use ==, always use ===.

Single-letter variable names are fine if it is blatantly obvious what they are. They are almost never that obvious.

If you have to write something nasty (it's JavaScript, it happens), write a little apology and explanation. If it's really gross, and you can prove that it's the best way to do it, we'll buy you a beer (or Soda of your choice) as a way to help you deal with the pain.

Clone this wiki locally