Interactivate Style Guide

Table of Contents

  1. Preface
  2. Variables
  3. Primitives
  4. Conditional Statements and Comparison
  5. Arrays
  6. Objects
  7. Functions
  8. Brackets and Whitespace
  9. Comments
  10. Semicolons
  11. Commas
  12. Naming Conventions
  13. Use Strict
  14. Linting

Preface

Except for PHP, Javascript is possibly the quirkiest and most annoying language you will come across. However, if done properly, it can be a joy to write code in. This guide will help you avoid common pitfalls and keep the puppy and unicorn fatalities to a minimum.

Compare the following two code blocks

//bad

function cp(a,b,c){return b*g*c/a}var g=.082;

//good


/**
* This is the ideal gas constant
* @constant {Number}
*/
var GAS_CONSTANT = 0.082;

/**
* Calculate the pressure of a function according to the ideal gas law pressure
* volume = moles * gas constant * temperature or PV = nRT
*
* @param {Number} volume The volume in liters
* @param {Number} amount The amount of gas in moles
* @param {Number} temperature The temperature in Kelvin
*
* @returns {Number} The pressure in atmospheres
*/
function calculatePressure(volume, moles, temperature) {
    return moles * GAS_CONSTANT * temperature / volume;
}

General Information

Variables

Primitives

In Javascript the primitive types are:

When you assign values to these, use the literal values

//bad
var num = new Number(3);
console.log(num); // => Number {}
var bool = new Boolean(true);
console.log(bool); // => Boolean {}
var str = new String("Hello, World");
console.log(str); // => String {0: "H", 1: "e", 2: "l", 3: "l", 4: "o", 5: ",", 6: " ", 7: "W", 8: "o", 9: "r", 10: "l", 11: "d", length: 12}
//Clearly, these are exactly what you wanted.

//good
var num = 3;
console.log(num); // => 3
var bool = true;
console.log(bool); // => true
var str = "Hello, World";
console.log(str); // => "Hello, World"

Conditional Statements and Comparison

//BAD CODE.
'\r\n\t' == 0;      // true. \r\n\t is a newline and a tab.
'' == '0';          // false
0 == ''             // true
0 == '0'            // true. What happened to the transitive property?

//GOOD CODE.

'\r\n\t' === 0      // false
'' === 0            // false
0 === ''            // false
0 === '0'           // false

Those are only some of a few examples of pitfalls of using ==. Use === and you will be fine

//bad
if(bool === true) {
    //do something if bool is true;
}
if(bool === false) {
    //do something if bool is false
}

//good

if(bool) {
    //do something if true
}

if(!bool) {
    //do something if false
}
//bad
if (typeof variable === 'undefined') {
    //define variable here
}

//good

if(variable) {
    //define variable here
}

Arrays

//bad
var array = new Array('Apple', 'Banana', 'Orange');

//good
var array = ['Apple', 'Banana', 'Orange'];
//bad
var arr1 = [1, 2, 3, 4];
var arr2 = [1, 2, 3, 4];

arr1 == arr2;   // false
arr1 === arr2;  // false

//good

function arrayEquals(array1, array2) {
    var i;

    if(array1.length !== array2.length) {
        return false;
    }

    for(i = 0; i < array1.length; i++) {
        if(array1[i] !== array2[i]) {
            return false;
        }
    }

    return true;
}

arrayEquals([1, 2, 3, 4], [1, 2, 3, 4]); //true. Note that if you have objects or arrays inside the array, you might need to make a deep equals
//bad
var array = [1, 2, 3, 4, 5];
for(var i in array) {
    print(array[i]);
}

//good

var array = [1, 2, 3, 4, 5];
for(var i = 0; i < array.length; i++) {
    console.log(array[i]);
}

Objects

//bad
var obj = new Object();
obj.prop1 = 'Hi';
obj.prop2 = 'Bye';

//good
var obj = {
    prop1: 'Hi',
    prop2: 'Bye'
};
//bad

var obj = {
    prop1: 'Hi',
    prop2: 'Bye'
};

alert(obj['prop1']);

//good

var obj = {
    prop1: 'Hi',
    prop2: 'Bye'
};

alert(obj.prop1);
//bad. 
var obj = {
    prop1: 'Hi',
    prop2: 'Bye'
};
var propName = 'prop1';

var property = eval('obj.' + propName); //Doing this is an awful idea. Don't do this. This is bad. This is evil. Never do this. This is awful.  ̕h̵i​s un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, evals lea͠ki̧n͘g fr̶ǫm ̡yo​͟ur eye͢s̸ ̛l̕ik͏e liq​uid pain, the song of eval parsing will exti​nguish the voices of mor​tal man from the sp​here I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful t​he final snuffing of the lie​s of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich​or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n​ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̾͛ͪ̈́̀́͘* ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M͊̒ͪͩͬ̚. If you̲̖̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ


//good code

var obj = {
    prop1: 'Hi',
    prop2: 'Bye'
};
var propName = 'prop1';
return obj[propName];
//bad
var obj = {
    a: 1,
    b: 2
};

for(var prop in obj) {
    console.log(prop + ": " + obj[prop]);
}

//good
var obj = {
    a: 1,
    b: 2
};

for(var prop in obj) {
    if(obj.hasOwnProperty(prop)) {
        console.log(prop + ": " + obj[prop]);
    }
}

Functions

//bad

function outer() {
    function inner() { //is not cross-browser
        console.log('hi');
    }
    inner();
}

//good

function outer() {
    var inner = function() {
        console.log('hi');
    }
}
function power(base, exponent) {
    var defaultBase = 2;
    var defaultExponent = 3;

    //if the parameters are undefined, set it to the default
    base = typeof base !== 'undefined' ? base : defaultBase;
    exponent = typeof exponent !== 'undefined' ? exponent: defaultExponent;

    return Math.pow(base, exponent);
}
power();        // 8
power(3, 4);    // 81
power(3);       // 3

Brackets and Whitespace

//bad
function fn(x){
var i;
for(i = 0; i < 10; i++){
console.log(i);
}
}

//worse
function fn(x)
{
var i;
for(i = 0; i < 10; i++)
{
console.log(i);
}
}

//good

function fn(x) {
    var i;
    for(i = 0; i < 10; i++) {
        console.log(i);
    }
}
//bad
var i;
for(i = 0; i < 10; i++)
    console.log(i); //what happens when you add another line to this?

//good
var i;
for(i = 0; i < 10; i++) {
    console.log(i);
}
//bad
var a=1;
var b=2;
console.log(a+b);
console.log(a ++);

//good
var a = 1;
var b = 2;
console.log(a + b);
console.log(a++);

Comments

//bad
function calculatePressure(v, n, t) {
    var r = 0.082;
    return r * n * t / v;
}

//This is valid code, but if you don't already know the formula, it is nearly impossible to understand. Compare it to the following code

//good

//calculate pressure according to the ideal gas law pressure * volume = moles * gas constant * temperature or PV = nRT
function calculatePressure(volume, moles, temperature) {
    var gasConstant = 0.082;
    return moles * gasConstant * temperature / volume;
}

//In the above example the code explains what it is doing, and the comments explain why it works and why we are using it
/**
* Calculate the pressure of a function according to the ideal gas law pressure * volume = moles * gas constant * temperature or PV = nRT
*
* @param {Number} volume The volume in liters
* @param {Number} amount The amount of gas in moles
* @param {Number} temperature The temperature in Kelvin
*
* @returns {Number} The pressure in atmospheres
*/

function calculatePressure(volume, moles, temperature) {
    var gasConstant = 0.082;
    return moles * gasConstant * temperature / volume;
}

Semicolons

Commas

//bad
var person = {
    firstName: 'Bob'
  , lastName: 'Joe'
};

//good

var i = 0,
    j = 3,
    k = 4;

var person = {
    firstName: 'Bob',
    lastName: 'Joe'
};
//bad
function(param1,param2,param3) {
    //do some stuff
}

//good
function(param1, param2, param3) {
    //do some stuff
}

Naming Conventions

//good
function Person(name) {

    //private variables prefixed by an underscore, but they aren't really private
    this._name = name;
}

Person.prototype.sayHi = function() {
    alert('Hi, my name is ' + this._name);
}
var person = new Person('Alice');
person.sayHi(); //alerts 'Hi, my name is Alice';

Use Strict

//good
function strict() {
    'use strict';
    //some strict code here
}

Linting

The excellent zalgo text taken from bobince