- Never mix tabs and spaces
- Use 2 spaces for indenting
- Never use
new to create a new variable. use [] for array, {} for object
// badvaritem=newObject(),array=newArray(),string=newString();// goodvaritem={},array=[],string='';- Always try to initiate variables in the first line
// badvarfoo=function(){function2();vara=1;function3(a);}// goodvarfoo=function(){vara=1;function2();function3(a);}// badvarfoo=1;varbar=2;// goodvarfoo=1,bar=2;
- Define private variables and private functions on top, add an empty line after private properties.
// bad(function(){varobj={};obj.foo=function(){bar();};i=0;bar=function(){i+=1;};returnobj;}());// good(function(){varobj={},i=0,bar=function(){i+=1;};obj.foo=function(){bar();};returnobj;}());- Chain function calls on new lines
//badanimate().now().then();//goodanimate().now().then();
- file names in smaller case with _ seperator
- Variables in smaller case with words separated by _
//badvarvariableNotLikeThis;varVariable_Not_Like_This;//goodvarvariables_like_this;
- functions should be in lower camelcase
//badfunctionIDoSomething(){}functioni_do_something(){}//goodfunctioniDoSomething(){}- Pseudo Classes should be in upper camelcase
//badvara=newthis_is_an_object();varb=newThis_Is_An_Object();//goodvara=newThisIsAnObject();
- Give necessary empty lines to make code more readable
//bad/* global ButtonView */'use strict';vara=require('foo'),b=1;c=function(){return2;},d=function(){return3};a.operation();returnb;//good/* global ButtonView */'use strict';vara=require('foo'),b=1,c=function(){return2;},d=function(){return3};a.operation();returnb;- break line on var chain if there will be a value assigned to it.
//badvara=foo(),b=bar(),c=1,d,e;//goodvara=foo(),b=bar(),c=1,d,e;