Why Aren't You Living in Harmony With ES6 Now?

When I first heard about the next version of JavaScript - known as ECMAScript 6 (ES6 for short) or JavaScript Harmony - I got really excited about all of the new features that we’d be getting to simplify and improve our lives as JavaScript developers… until I thought about it for 5 more seconds and realized that my kids would probably all be grown and moved out before I didn’t need to support any browsers that don’t support ES6, so I may never actually get to use it. I’m discovering that ES6 might be more usable right now than I thought it would be.

Node.js is Harmonious

Node.js runs its JavaScript code on the V8 engine developed by Google. Since the V8 engine has some ES6 features enabled, we’re able to run our Node.js code using the ES6 features that are currently available in the V8 engine. Sadly, there aren’t a whole lot of features implemented yet:

  • Block scoping
  • ES6 semantics for typeof
  • let and const
  • Map and WeakMap
  • Module declaration
  • The Proxy prototype

In order to use the ES6 goodies, you need to run node with some flags. Alex Young from DailyJS describes how to use ES6 in Node.js, and even though the article getting pretty aged, the content seems pretty consistent with the current state of Node.js and V8.

Traceur Transpiling

Traceur is a project by Google that compiles ES6 code into ES5 code. It also comes with a small 23KB (minified, not GZipped) file that offers all of the utility code you need to get the compiled code to do all the cool things that ES6 does. While developing, you can include a much larger file instead, which will compile the code in the browser so that you can make quick changes without the need to compile again before refreshing the browser.

Currently, Traceur supports a lot more ES6 features than is available in any of the JavaScript engines, which makes it a great choice if you really want to use most of what ES6 has to offer.

To compile your code, you can either use the Traceur compiler (available through NPM with npm install -g traceur on the command line) directly or you can set up Grunt or Gulp to do it. The Traceur compiler is a lot easier to get started with, so it’s good for experimenting to see if you like it, but once you’re using Traceur consistently in your projects, you’re a lot better off getting a task runner to handle it for you because they can watch your files for changes and automatically compile any time you save an ES6 file.

Conclusion

I’m sorry I didn’t go into much detail about using Traceur, but I will be giving a more thorough tutorial on using it soon enough. You should be able to see, though, that ES6 is ready to use now, so don’t sit around moping that you’ll never really get a chance to use it in a project. God bless and happy coding!

Author: Joe Zimmerman

Author: Joe Zimmerman Joe Zimmerman has been doing web development ever since he found an HTML book on his dad's shelf when he was 12. Since then, JavaScript has grown in popularity and he has become passionate about it. He also loves to teach others though his blog and other popular blogs. When he's not writing code, he's spending time with his wife and children and leading them in God's Word.