Skip to content

JavaScript Code Etiquette

Ian-Stewart-Binks edited this page Oct 4, 2014 · 21 revisions

#JavaScript Coding Style Guide#

###var###

  • No implicit var declarations.
  • No duplicate variable declarations or shadowing.

###Semicolons###

  • Always use semicolons. The JavaScript engine has the ability to infer semicolons, but omitting them is frowned upon.

###Spacing in equations###

  • Spacing in equations is necessary. var x=2+2*65/2 < var x = 2 + 2 * 65 / 2. (Parenthesis would be good in this situation to- but that's for a different style tip).

###Function spacing###

  • Put a double space in between function declarations.

###Variable naming###

  • CONSTANTS_ARE_IN_UPPERCASE.
  • For now, the rest are in camelCase.

###jQuery selector caching###

  • Duplicated selectors can and often should be cached for efficiency. #!javascript var circleObject = $("#circle");

###Function naming###

  • camelCase.
  • Imperative voice (Things such as setMapSize())

###Line breaking###

#!javascript
var 1 + 2 + 3 + 
  4;

is preferred over

#!javascript
var 1 + 2 + 3
  + 4;

likewise,

#!javascript
if (i === "i" or
    i === "j") {
...
}

is preferred over

#!javascript
if (i === "i"
    or i === "j") {
...
}

###Quotation marks###

  • ' is preferred over ".