Skip to content

Instantly share code, notes, and snippets.

@pkyeck
Forked from sindresorhus/codestyle.md
Created December 13, 2012 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkyeck/4275522 to your computer and use it in GitHub Desktop.
Save pkyeck/4275522 to your computer and use it in GitHub Desktop.

Code Style

  • Space indentation (2 spaces)
  • Double-quotes
  • Semicolon
  • Strict mode
  • No trailing whitespace
  • Variables at the top of the scope
  • Multiple variable statements
  • Space after keywords and between arguments and operators
  • Always use curly braces
  • Return early
  • JSHint valid
  • Consistency

Example:

"use strict";

function foo(bar, fum) {
  var i, l, ret;
  var hello = "Hello";

  if (!bar) {
    return;
  }

  for (i = 0, l = bar.length; i < l; i++) {
    if (bar[i] === hello) {
      ret += fum(bar[i]);
    }
  }

  return ret;
}

Read idiomatic.js for general JavaScript code style best practices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment