Skip to content

add ES2015 variables #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion languages/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,32 @@ if (foo === bar) {
}
```

If the environment you're running in supports ES2015 (Node.js 4.x/[Babel](https://babeljs.io/)) then you should not use `var` statements at all – you should be using `let` and `const`:

In ES2015 we do this:

```js
const foo = 'bar';
```

We use `let` only when a variable _explicitly_ [needs to be mutable]:

```js
let foo = 'bar';
```

In ES2015 we _don't_ do this:

```js
var foo = 'bar';
```

You can also use destructuring assignment with objects and arrays. These don't have to be multi-line, but there must be spaces after the commas:

```js
let {foo, bar} = getFooAndBar();
let [baz, qux] = getBazAndQux();
```

Client-Side JavaScript Architecture
-----------------------------------
Expand Down Expand Up @@ -302,4 +328,4 @@ An example utility might be a function to make a string title-case.
[eslint]: http://eslint.org/
[jshint]: http://jshint.com/
[hoisting]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting

[needs to be mutable]: https://ada.is/blog/2015/07/13/immutable/