My team recently upgraded our Node version from 0.10 to 4.0 for a big app we are working on. Lots of improvements came in the upgrade, but by far, my favorite is all of the ES6 features now available natively in Node.
I thought I would go over a few of my favorite features, focusing on what is available and why I like it, rather than the specifics of each feature. There are a huge number of resources like MDN which can explain the specifics better than I can!
let and const
If I’m being honest, var isn’t that bad. Functional scoping is admittedly weird, but I don’t know that I’ve ever encountered an actual bug from it. let just makes more sense though; it works like every other programming language.
This is my absolute favorite ES6 feature. Arrow functions solve the issue of anonymous functions failing to bind the
this variable correctly. As a bonus, they also have a much more succinct syntax. They play really well with classes and libraries like lodash.
Promises are fantastic, and now you no longer need a separate library for them. ES6 native promises follow the same Promises/A+ standard as all the most popular libraries, so they can interop seamlessly with them.
Things You Don’t Get
Node, at least in version 4.0, does not support all of ES6. Also, most of the features are only available in strict mode, but you’re already using that, right? Some of the great ES6 features you don’t get include:
- Default function parameters
- Array and object destructuring
For an exhaustive list of what is and isn’t supported, check out this table.
But honestly, the things you do get are a big win, as is not having to run your code through a compilation step to get it. If you aren’t using ES6 in your Node.js apps, now is the time to start!
Not to mentioned generator functions! Generator functions wrapped with the co library can really cleanup callback and promise syntax.
I’ve really been enjoying using some of the new es2015 features, too. I’ve started transpiling my server code with babel on my most recent project, so as to better support isomorphic React apps. It’s nice because it avoids that “big bang” loading effect of SPAs where the app is initially displayed in a neutral state, then makes multiple API calls to load data.
It looks like they’re dropping future plans for array comprehensions. They’re one of my favorite features of python.
Woohoo node.js 6.0.0 released this week, with 96% native ES6 coverage!
Comments are closed.