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.
  • Duplicate declarations: I don't like them, but to be discussed. A bit too shadow-esque! However, I don't know why they couldn't be good.

###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###

  • I double space functions, but that's my own preference and I haven't looked up the suggested way to do it. problem.js uses both single and double spaces. We should choose one- what are your thoughts?

###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 ".