-
The code uses proper white space and indentation
$(document.ready(function() {
$('#test').click(function() {
console.log('this is not very readable');
});
});
$(document.ready(function() {
$('#test').click(function() {
console.log('this is better');
});
});
-
Each statement terminates in a semi-colon
function alertMe() {
alert('hello') //missing semi-colon
}
-
There is only one statement per line
var index = 0; index++; alert(index); // avoid
//this is more readable for anything but the simplest statements
var index = 0;
index++;
alert(index);
-
All variables are declared with var
var index = 0;
-
The code avoids the eval() statement
var userInfo = eval(feed); //no!
-
The code uses single quotes
var name = 'Bob';
-
The for... in loop is only used to iterate through properties of objects (not arrays)
for (var prop in myObj) {
//do something with myObj[prop]
}
-
Functions and unclear statements are documented with code comments
var zip = elements[1]; //zip code is returned from API in position 1
-
Variable names are camelCase and are noun phrases
var firstName = "George";
-
Function names are lowercase and are verb phrases
function getName() { return firstName; }
-
Class names are SentenceCase and are noun phrases
function Car() { ... }
-
The code avoids redundant checks for boolean values
if (y != null && y != '') { ... }
if (y) { ... } //does the same thing and is much shorter
-
The strict operator ("triple equals") is used when making comparisons
if (name === 'Bob') { ... } //not ==
-
The code within a conditional or iterator block (if, else, for, while etc.) is surrounded by braces
for (var i = 0; i < arr.length; i++)
console.log(arr[i]); //this technically will not cause an error but should be avoided
-
The jQuery code makes use of function chaining where appropriate
$('a.selected')
.attr('id', 'searchLink')
.css('color', '#fff')
.click(function() {
...
});
-
No line of code is more than 80 characters
// for your reference, this sentence demonstrates what 80 characters looks like