Skip to content

Javascript

Adam Norris edited this page May 14, 2018 · 1 revision

Favour readability over brevity in variable names

Always declare variables with the var keyword unless using ES2015 binding constructs (let and const).

Use the literal syntax to declare types

// Don't Use
var myArray = new Array();
var myObject = new Object();

// Safer
var myArray = [];
var myObject = {};

Always use the === comparison operator unless you specifically want to make use of type coersion.

// Only in certain circumstances
if ( 5 == '5' );
if (3 != '3');

// Safer
if ( 5 === '5' );
if (3 !== '3');

Use camelCase for naming variable and functions

Be careful not to pollute the global scope. Wrap any code in a closure.

// Prevents variables leaking
(function(){
  // place code here
}());

Place scripts at the bottom of the page to prevent render blocking unless there is a specific reason it needs to be in the head (e.g. Modernizr - in some cases)

Libraries

Authors should use libraries sparingly and try to justify the cost of adding extra dependencies vs the feature they are building.

Any included JS library should be minified and compressed for production and ideally customised to suit the needs of the situation (e.g. only loading required Modernizr/Bootstrap modules).

Clone this wiki locally