- 2 spaces - for indentation
- Single quotes for strings - except to avoid escaping
- No unused variables - this one catches tons of bugs!
- No semicolons - It's fine. Really!
- Never start a line with
(,[,
or ````` !- This is the only gotcha with omitting semicolons - they are automatically checked for you!
- More details
- Space after keywords:
if (condition) {...}
- Space after function name:
function name (arg) { ... }
- Always use
===
instead of==
- The use of
obj == null
is allowed to check fornull || undefined
- The use of
- Always handle the
node.js err
function parameter - Always prefix browser globals with
window
- Except for
document
andnavigator
, which are okay.- This prevents accidental use of poorly-named browser globals like
open, length, event,
andname
.
- This prevents accidental use of poorly-named browser globals like
- Except for
- And more goodness – give
standard
a try today!
To get a better idea, take a look at a sample file written in JavaScript Standard Style. Or, check out one of the thousands of projects that use standard!
Courtesy of StandardJS.com</cite>
JavaScript Style Recommendations from Meteor
An example of refactoring from JavaScript to ES2015
ECMAScript, the language standard on which every browser’s JavaScript implementation is based, has moved to yearly standards releases... Meteor’s ecmascript
package compiles this standard down to regular JavaScript that all browsers can understand using the popular Babel compiler. It’s fully backwards compatible to “regular” JavaScript, so you don’t have to use any new features if you don’t want to. We’ve put a lot of effort into making advanced browser features like source maps work great with this package, so that you can debug your code using your favorite developer tools without having to see any of the compiled output.
The ecmascript
package is included in all new apps and packages by default, and compiles all files with the .js file extension automatically. See the list of all ES2015 features supported by the ecmascript package.
To get the full experience, you should also use the es5-shim
package which is included in all new apps by default. This means you can rely on runtime features like Array#forEach
without worrying about which browsers support them.
We recommend choosing and sticking to a JavaScript style guide and enforcing it with tools.