JavaScript Style

Please see feross/standard’s RULES.md for a general guide of form to follow for javascript. One of the fundamental concepts is the elimination of semicolons but there are some caveats.

Beside that:

import/require of classes should be in UpperCamelCase, even if they are not:

// OK
const Path = require('path')

// Avoid
const path = require('path')

Avoid multiple named imports on one line:

// OK
import {
  thing1,
  thing2
} from 'whatever';

// Avoid
import { thing1, thing2 } from 'whatever'

Avoid defining anonymous static object as function parameter. This improves debugging and avoid Promise-related clutter but I understand the shortcut:

// OK
const myObject = {
  hello: 'Hi!'
}
someFunc(myObject);

// Avoid
someFunc({
  hello: 'Hi!'
})

Avoid checking boolean values with expressions that check for value:

const myBoolean = true;

// OK
if (myBoolean) {}

// Avoid
if (myBoolean === true) {}

Prefer => when using Promises or callbacks.

Use indentation with Promises to improve readability:

// OK
myFuncThatReturnsPromise()
  .then(res => someTransform(res, 'something'))
  .then(console.log)
  .catch(console.error)

// OK
SomeStaticClass
  .myFuncThatReturnsPromise()
  .then(res => {
    console.log(res)
    return someTransform(res, 'something')
  })
  .then(console.log)
  .catch(console.error)

// Avoid - `then` on same line
myFuncThatReturnsPromise().then(res => someTransform(res, 'something'))
  .then(console.log)
  .catch(console.error)

// Avoid - not using arrow and not properly indented
myFuncThatReturnsPromise().then(function (res) {
  return someTransform(res, 'something')
})
.then(console.log)
.catch(console.error)

Nice Things

  // Best
  return {
    user:         state.user,
    channel:      state.channel,
    newuser:      state.newuser,
    creditcard:   state.creditcard,
    coupon:       state.coupon,
    queryparams:  state.queryparams,
  }

  // OK
  return {
    user: state.user,
    channel: state.channel,
    newuser: state.newuser,
    creditcard: state.creditcard,
    coupon: state.coupon,
    queryparams: state.queryparams,
  }

TIPS

This is ugly, why the JS folks do not want to improve this potentially awesome expression is beyond me.

var select = (x => {
  switch(x) {
    case 'A': return 0
    case 'B': return 1
    default:  return 9
  }
})(y)