JavaScript best practice summary – Jonathan Mills from Pluralsight

Syntax – consistency is the king.

  • Use semicolons.
  • Use Linting. e.g. ESLint or JSHint
  • Curly braces should start in the same line of command, which is different from C#, etc.
  • Equality. Use === whenever possible.
  • Write variable, function, caller in order.

Behaviors – use strict mode.

  • Use var if it’s not for a global variable
  • Use ‘use strict’;  This will remove weird JS behavior and throw an error rather than ignore silently.
  • Use ‘var _this = this’ pattern at the beginning of the object when creating a function object so as not to lose this scope inside the object.

Async Patterns

  • Callbacks
    • Use the named function.
    • Return callback function like ‘done’.
  •  Promises
    • Use promise from promisejs, which can lead to monads.
  • ES6 and Babel
    • Use ES6 rather than ES5
    • To use ES6 set up the environment using Babel which transpile ES5 JS to ES6 JS.
  • Async Await
    • New stuff

 

Leave a comment

Your email address will not be published.