Last active
August 29, 2015 14:07
-
-
Save vamshisuram/9999d4238d4a3c68e467 to your computer and use it in GitHub Desktop.
js coding standards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Indentation - 2 spaces (tabs converted to spaces) | |
| Line length - 80 | |
| comments up-to-date | |
| block comments for documentation; typical - line comments | |
| var variable; (each variable in a new declaration) | |
| // named functions - mind the spaces; alignment of curly braces | |
| function name() { | |
| } | |
| // anonymous functions - mind the spaces; alignment | |
| function () { | |
| } | |
| // function invoked immediately, returning value | |
| var a = (function () { . . . }()); | |
| Naming private members with _ (AVOID) | |
| variables, functions - names in (camelCase) | |
| constructor fuctions - names in (PascalCase); used with new prefix | |
| global variables - names in (UPPERCASE) | |
| simple statement ending with semicolon | |
| compound statements - left curly brace at the end of the line begins the statement; content indented | |
| return statements - no parenthesis around the return value; return value expression starts in the same line | |
| // spaces check | |
| if (condition) { | |
| statements | |
| } else { | |
| statements | |
| } | |
| if (condition) { | |
| statements | |
| } else if (condition) { | |
| statements | |
| } else { | |
| statements | |
| } | |
| for (intialization; condition; update) { | |
| // for arrays of definite size | |
| } | |
| for (variable in object) { | |
| // for objects | |
| } | |
| for (variable in object) { | |
| if (object.hasOwnProperty(variable)) { | |
| statements | |
| } | |
| } | |
| while (condition) { | |
| statements | |
| } | |
| do { | |
| statements | |
| } while (condition); // semicolon check | |
| // each case is aligned with switch | |
| switch (expression) { | |
| case expression: | |
| statements | |
| break; // break, return, or throw | |
| default: | |
| statements | |
| } | |
| try { | |
| statements | |
| } catch (variable) { | |
| statements | |
| } | |
| try { | |
| statements | |
| } catch (variable) { | |
| statements | |
| } finally { | |
| statements | |
| } | |
| Avoid use of the continue statement. | |
| var a = {}; //instead of new Object() | |
| var b = []; //instead of new Array() | |
| Use the === and !== operators | |
| var total = subtotal + (+myInput.value); | |
| Avoid eval. | |
| eval has aliases. Do not use the Function constructor. Do not pass strings to setTimeout or setInterval. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment