Skip to content

JavaScript Style Guide

connorshea edited this page Nov 29, 2014 · 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.

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

Comment methods of an object in Javascript above declaration.

function do_thing()
{
  return function(arg){
    attack(x);
  }
}

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

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

Conventions, constructs, and naming

Never use ==, always use ===.

Only use single-letter variable names x, y, and z for things you expect to be numbers.

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).

Clone this wiki locally