Javascript Style Guide


Use single quotes unless you are working with JSON.

Declare one variable per var statement:

var x = 5;
var y = 1;

Open braces go on the same line as the statement:

if (true) {

Variables and methods should use lower camel case. The should also be descriptive. Single character variables and uncommon abbreviation should be avoided.

Class names should be capitalized using upper camel case.

Constants should be declared as regular variable or static class properties using all upper-case letters.

Do not extend the prototypes of any objects, especially the ones you do not own (the native ones).

Non-trivial conditions should be assigned to a descriptive variable:

var isAuthorized = (user.isAdmin() || user.isModerator());
if (isAuthorized) {

Keep function definition short. A good function fits on a slide =~ 10 lines of code per function.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License