JavaScript standard/standard Linter


The standard/standard Style Guide and Linter tool is a JavaScript module licensed by MIT and conveniently available through npm. It can be integrated into testing frameworks, or executed on it’s own. Either way, it validates your code against their style guide, and throws warnings as appropriate:

  • 2 spaces – for indentation
  • Single quotes for strings – except to avoid escaping
  • No unused variables – this one catches tons of bugs!
  • No semicolonsIt’sfine.Really!
  • Never start a line with (, [, or `
    • This is the only gotcha with omitting semicolons – automatically checked for you!
    • More details
  • Space after keywordsif (condition) { ... }
  • Space after function namefunction name (arg) { ... }
  • Always use === instead of == – but obj == null is allowed to check null || undefined.
  • Always handle the node.js err function parameter
  • Always prefix browser globals with window – except document and navigator are okay
    • Prevents accidental use of poorly-named browser globals like open, length, event, and name.
  • And more goodnessgive standard a try today!

( “StandardJS – The Rules”)

The tool features a “–fix” flag, when used, it will automatically apply formatting to your code, conforming to these rules.

It is not the official ECMAScript standard, as the name “standard” may imply. However, it does has quite the following, being used by npm, GitHub, and mongoDB just to name just a few. It launched in January 2015 and has currently undergone 150 releases, with over 1,227 commits by 102 contributors. The founding author and major contributor is Feross from San Francisco.

This initially caught my interest because I have not used a formatting tool before. My style differs from a few of these rules. (Uh-oh… is my style terrible?). With a little google searching, I see it’s a highly debated subject. So, this seems useful from a consistency perspective. No need to argue if everyone agrees to run the same tool. Not only that, it appears to add considerable value preventing unseen bugs by adding these checks to your test cases.

I’m excited to try this out on one of my projects and perhaps share the details here with you.

That’s all for now, take care!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s