Although ES6 (aka EcmaScript 2015) is not yet widely supported in browsers, I thought it might be helpful to provide some base information to help readers (particularly my students) better understand how to work with ES6 (and discuss a couple of the features). I got this idea from taking the course Up and Running with ECMAScript 6 at lynda.com. However, as I worked through the examples provided, I quickly discovered that a new version of Babel has come out since the course was developed (oh sure, that never happens in my classes… oh… wait… every semester). Again, my purpose is to introduce you to the extreme fundamentals of working with ES6. I plan to provide a number of added posts in coming months delving into much greater detail.
First things first. We need an environment to run the examples. In my case, I am using a Mac (which is why the screen captures look the way they do). If you are using a Windows machine, you will need to first install Node Package Manager (npm). There is a Windows installer. Assuming you have that step out of the way, we next need to install babel.
Of course, the latest version of babel moved to babel-cli. Within terminal (on a Mac) or the command shell (on Windows), we need to issue the command:
npm install -g babel-cli
The -g switch makes this global (for all users of the machine). You should see something like the following:
Of course, the latest version of babel does not come with any presets. This is why (if you try to follow the examples in the above lynda.com tutorials, you will be very frustrated). You need to install the ECMAScript 2015 (aka ES6 preset).
npm install babel-preset-es2015 --save-dev
Your results should look similar to what is shown below.
Now we have a working platform. Yes, there are better ways to do all of this. Keep in mind, I wanted to focus on the absolute simplest approach so I can discuss a bit of ES6 in the process.
First, let’s look at one of the things we can do in ES6 that we can not do in the current version (supported in most browsers). This is the same example provided in the above lynda.com course. We can now have default function parameters. This means that if we call a function and do not pass any arguments, there can be defaults. Yes, this is a big deal (and a long time coming).
Let’s look at a bit of code (screen capture from Brackets) [again, this is only slightly modified from the example provided in the lynda.com course mentioned above]. On line 4, we create a new function called nameBuilder. We also provide default values for both firstName and lastName. We then write to the console log the results of the function call.
We then call the function nameBuilder on
- line 8 – passing no arguments
- line 9 – passing one argument (which will default to the first)
- line 10 – passing 2 arguments
Of course, if we try to run this directly in our browser, it will presently throw an error. Instead, we need to transpile the ES6 code using babel so it will work properly today. We do this with the code below:
babel script.js --presets es2015 --out-file script-compiled.js
As you can see, the code has expanded quite a bit. Let’s look at the major changes:
- line 1 – we must specify “use strict” so the code is interpreted properly
- lines 7 and 8 – we have to properly account for the possible arguments. Note also the use of the triple equals operator (identity – must be an exact match including type).
As you see, line 13 had no arguments passed. This resulted in the default values being used.
Line 14 had only one value passed. This means we ended up with Fred DuBois (because only the first value was replaced).
Line 15 passed both values and resulted in Fred Derf being displayed in the console log.