# Postmates JavaScript/JSX Style Guide

Unless otherwise noted, everything in this style guide is referring to ES6+.

## Variables

### Declaration

Group variable declarations together when possible. Use `const` if the variable will not be reassigned. Sometimes, a variable is not ready to be assigned at the top of its scope, so its declaration should be separate from the group.

Don't use `var`. Use `let` or `const`. Note that `let` and `const` variables are block-scoped, so declare variables within blocks where it makes sense (for-loops, `if` blocks, etc).

```javascript
// bad
const a = 2;
const b = 3;
let c;

// good
const a = 2,
    b = 3;
let c;

// good
const a = 'foo';
let b = 'bar';

if (/* condition */) {
    b += ' bat';
}

const c = `${a} ${b}`; // only assigned once but cannot be declared at the top
```

#### ES5

Since variables are only function-scoped in ES5, always declare variables at the top of the function scope.

```javascript
// bad
function(x) {
    var a = 0;

    if (x > 0) {
        var b = a + x;
    }
}

// good
function(x) {
    var a = 0,
        b;

    if (x > 0) {
        b = a + x;
    }
}
```


## Utility Functions

Since we are using babel to transpile ES6 (and beyond), prefer native methods over external libraries such as Lodash or Underscore. Many functions offered by `_` can be replaced by native methods (`Object.values`). For utility functions that are not provided natively (such as `flatten`), consider adding them in a shared repo...with comprehensive tests.
