JavaScript Best Practice


General Recommendations
  • Avoid global variables.
  • Define all local var's at the top of each function.
  • Use explicit comparators (ie “===”) over coerced comparators (ie “==”).
  • Use shortcuts for default value assignment.
  • Always put braces on the right of block definitions.
  • Always use semi-colons at the end of a line.
  • Never leave dangling commas in lists and object definitions.
  • Never use “eval()”.
  • Never leave debugger statements in code.
  • Always follow standard variable and function naming conventions.
  • Never use hyphens in variable names.
  • Never test variables against text strings for triggered events.
  • Never hard code strings within program code or templates.
  • Always use double quotes for NLS'd strings.
  • Never hard code usernames or passwords in client processed code.
  • Never persist passwords locally.
Logging and Comments
  • Use standard logging with useful details.
  • Use the proper logging levels.
  • Use multiple arguments to log, not string concatenation.
  • Comment all complex logic.
  • Use JSDoc syntax for all public module, function, and variables.
Performance
  • Be suspicious of logic inside loops.
  • Keep the DOM as small as possible.
  • Minimize DOM access / manipulation.
  • Ensure all 3rd party libraries are optimized.
Thanks for your comment