CHAPTER 1
ECMAScript 6 (ES6), also known as ECMAScript 2015, brings new functionality and features to the table that developers have been wanting for a long time. The wait is over, and you can now develop all of these features. The really good news is that you can also target browsers that don’t even support ES6 yet using a transpiler. A compiler translates one language to another, such as C# to MSIL, while a transpiler converts one version of a language to another, such as ES6 to ES5.
In order to follow along with the code samples in this book, you will need to have the following on your development machine:
It is possible to run most of the code examples in the browser using your favorite transpiler, but for the sake of consistency we will be using previously stated dependencies. It is our goal to also provide you with a quick and easy tool to help you get the most out of understanding the new features in ES6.
Note: Unless otherwise indicated, we are going to use Sublime Text along with Babel and Node.js.
The following are the steps to get your machine ready for using the code examples in this book:
Code Listing: 1
{ "cmd": ["babel-node", "$file"], "path": "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin" } |
Code Listing: 2
{ "shell_cmd": "babel-node $file", "path": "$PATH" } |
Code Listing: 3
let greeting = "hello world"; console.log(greeting); |
Note: You may need to place a 'use strict' statement as the first line in file test.js in order to get the script to run.

Figure 1: Sublime Text output
If you find that it is not recognizing the babel-node command, I would suggest installing the following globally so that path environment is updated correctly:
Code Listing: 4
npm install –g babel npm install –g babel-cli |
If you don't wish to use Sublime Text as your editor and build system, you can simply use any text editor (such as Notepad), save your source code with a .js extension, navigate to the directory containing your script, and then execute from a command line by typing:
> babel-node myscript.js
Another great tool for testing ES6 capabilities is ScratchJS. It is an extension for Chrome and even allows you to select which transpiler to use. Again, you can use your own tool of choice, but when we get to Modules we want to load modules from the file system, and this makes it hard when using a browser testing tool.
The following is a short list of popular transpilers:
Desktop browser support is coming along pretty well. You will be surprised to know that Microsoft Edge is right on the tail of Firefox as leading the way in standards compliance. Chrome is close behind but still has some ways to go.
As of writing this book, it is recommended to use a ‘transpiler’ such as Babel.
All code listings in this book have been generated using Sublime Text 3. Most of the code examples and screenshots were created using Scratch JS in Chrome. Scratch JS is an extension for Chrome; it allows you to pick which transpiler you wish to use. For the remainder of this book, except where noted, we will be using Babel as the transpiler. Scratch JS also allows you to toggle the output of your ES6 code down to ES5 code by clicking Toggle Output. This can be beneficial if you are curious as to what the syntax of the code would look like in classic ES5 code. Because ES6 is still under development, you may run into some minor problems getting some of the examples to work, especially if you call babel-node from a command line. If this happens, you can go to the Babel website, and paste ES6 code into their interactive transpiler page. This will generate ES5 code. You can take this output, save it as a .js file, and run it using node. This approach usually solves most issues. Finally, you can also execute your ES6 code from the Scratch JS tab by clicking Run.