Skip to content

Instantly share code, notes, and snippets.

@js62789
Last active May 1, 2018 22:03
Show Gist options
  • Save js62789/2ab652311c9d49b567c9 to your computer and use it in GitHub Desktop.
Save js62789/2ab652311c9d49b567c9 to your computer and use it in GitHub Desktop.

Naming Conventions

  • Use PascalCase when naming constructors
function Person(name) {
  this.name = name;
}
  • Use all capitals when defining constants
Person.BloodTypes = {
  A: 1,
  B: 2,
  AB: 3,
  O: 4
};
  • Use camelCase when naming objects, functions, and instances
var someData = {};
  • Use named function expressions so that the stack trace doesn't show a series of anonymous functions
var doSomething = function doSomething() {

};
  • Start a method name with an underscore if it is intended to be a private method
Person.prototype._log = function () {
  console.log.apply(null, arguments);
};
  • If a file exports a single module, the filename should be the same as the module name
// Person.js
function Person() {}
module.exports = Person;

Documentation

  • Documentation of modules should follow the JSDoc format. If using Sublime Text, the DocBlockr plugin is recommended. Documentation is achieved through comments and self-documentation.
  • Use /** ... */ when documenting multi-line comments
/**
 * This is a comment that
 * spans multiple lines
 */
  • Use // when documenting a single-line comment
// This is a comment that fits on one line
  • Annotate any known problems with a TODO flag
// TODO This should be doing something
var doSomething = function doSomething() {};

Declarations

  • Never declare multiple variables using the same var operator unless every variable declared is undefined
// bad
var one = 1, 
  two = 2, 
  three;

// bad
var one = 1, 
  two, three;

// good
var one = 1;
var two = 2;
var three = 3;

// good
var one = 1;
var two, three;
  • Use literal syntax for object and array creation
// bad
var obj = new Object();
var arr = new Array();

// good
var obj = {};
var arr = [];
  • Avoid using reserved words and wrap in quotes when they can't be avoided
var options = {
  status: 1,
  "default": {}
};
  • Strings should be declared with single quotes
// bad
var str = "This is a string";

// good
var str = 'This is a string';
  • Declarations should always end with a semicolon
// bad
var doSomething = function doSomething() {}
var str = 'Something'

// good
var doSomething = function doSomething() {};
var str = 'Something';

Format

  • Indentation should consist of two-space tabs

  • Files should end with a single newline characters (use {"ensure_newline_at_eof_on_save": true} in Sublime Text)

  • Leave a space before opening braces, parenthesis, and operators, as well as after operators

// bad
var x=y+z;
function Person(){}
if(true){}

// good
var x = y + z;
function Person() {}
if (true) {}
  • Use indentation when making long method chains, while using a leading dot to signify a continuation
doSomething()
  .then(function(){})
  .fail(function(){});
  • Leave space before and after a block
var str = '';

if (!str) {
  str = 'something';
}

return str;
  • If operators should exist on the same line as the opening brace and else operators should exist on the same line as the closing if brace and opening else brace
// bad
if (str)
{

}
else
{

}

// good
if (str) {

} else {

}

JSHint Rules

{
  "bitwise":    true, // Don't use bitwise operators
  "curly":      true, // Use curly braces around blocks
  "eqeqeq":     true, // Use === and !== instead of == and !=
  "es3":        true, // Adhere to ES3 standards
  "freeze":     true, // Don't overwrite prototypes of native objects
  "immed":      true, // Wrap IIFE's with parenthesis
  "indent":     2,    // Indent with two spaces
  "latedef":    true, // Define a variable before it's used
  "newcap":     true, // Capitalize constructors
  "noarg":      true, // Don't use arguments.caller and arguments.callee
  "nonbsp":     true, // Don't use non-breaking whitespace characters
  "nonew":      true, // Don't call constructor without assigning to variable
  "undef":      true, // Don't use an undeclared variable
  "trailing":   true  // Remove trailing whitespace
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment