- always declare variables with "var"; Without "var" the variable gets placed in the global context, and could override existing values
- always use semicolons
- always use curly braces; even for 1-line code
- do not use eval(), with() and void()
- always use '===' and '!==' instead of '==' and '!='
- always use radix param in parseInt('09', 10);
- use Number('8.5') to convert string to number
- declare arrays like this: var arr = [1, 2, 3];
- declare object like this: var obj = {a: 0, b: 1};
- do not modify prototypes of builtin objects
- use single quote (') instead of double quote (") in strings
javascript code conventions
var a = 5, b = 10;
var arr = [], obj = {};
if (a === b) {
console.log('a === b');
} else { // not equals
console.log('a !== b');
}
for (i = 0; i < arr.length; i += 1) {
console.log(i);
}
function f() {
return true;
}
f = function (e) {
return true;
};
obj = {
method: function () {
return this.data;
},
data: 0
};
Pingback: JavaScript | Заметки по программированию