javascript style guide

  1. always declare variables with "var"; Without "var" the variable gets placed in the global context, and could override existing values
  2. always use semicolons
  3. always use curly braces; even for 1-line code
  4. do not use eval(), with() and void()
  5. always use '===' and '!==' instead of '==' and '!='
  6. always use radix param in parseInt('09', 10);
  7. use Number('8.5') to convert string to number
  8. declare arrays like this: var arr = [1, 2, 3];
  9. declare object like this: var obj = {a: 0, b: 1};
  10. do not modify prototypes of builtin objects
  11. use single quote (') instead of double quote (") in strings

javascript code conventions

'use strict';
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
};

1 comment on “javascript style guide

  1. Pingback: JavaScript | Заметки по программированию

Leave a Reply

Your email address will not be published. Required fields are marked *