What's New in EcmaScript 6

Postado em - Última vez Modificado em

The ECMAScript 6 standard (ES6) was finalized in June of 2015. If you work with this language, you'll be excited about this because the ES6 version solves a lot of the headaches—ones that caused many developers to think that JavaScript isn't a good programming language. This article will walk you through the main features of ES6. Firefox Developer Edition (v40) and Chrome Beta has already released support for most of the features presented here.

Let's start with some of the most basic features, such as declaring variables. As most of you probably know, variables are available globally or in the function's scope in Javascript. In ES6, there can be declared block level variables using the let keyword.

let keyword

Many JavaScript developers say that by the time ES6 is supported by the majority of browsers, let should be the only way to declare variables in JavaScript and we should forget the var keyword.

Another great feature in ES6 is the possibility to declare real constants using the const keyword and there is no need for object properties definitions to make values constant. As you can see on the console of the FF40 I got a SyntaxError: invalid assignment to const myAge.

const keyword

In case you got annoyed like I did about the number of times the function keyword needs to be written, then you will appreciate the next feature – arrow functions (in other languages like C# these are called lambda expressions). This notation – (variable) => { ... } – saves a lot of typing and results in easier and more readable code.

arrow functions

In the code above, the function created by using the new notation can be used the same way it was used before (I execute it right after creating).

The next syntactic sugar that reduces the amount of written code is the property shorthand notation. Let's take a look at the code below:

property short hand notation

As you can see, I only specified the variables at object creation when I created the person object, and there was no need to explicitly write the field names. Of course this is valid if you are OK with the names of variables that you have.

Finally, I arrive at one of the most important new features: support for object-oriented programming. In ES6 you can create classes, there is inheritance, there are getters and setters and there are static class members. (Sadly the FF40 does not fully support class creation, but the code should be valid according to the standard).

In the code, I created a new class (Person) that has a constructor with two parameters (name and age). I defined two getters for the name and age and I defined a new method (getsOlder) that increases the age fields value.

classes in OOP

Next, I created a subclass of the Person class where I used a static method, which afterwards is used as a builder function.  

classes part 2

Besides the main features that I presented here there are many other, minor changes that can help developers and bring JavaScript up to the standards of other programming languages. These features include string interpolation, better support for export and import of symbols, generator functions (such as the presence of yield keyword), and set and map data structures.

The new ES6 standard should also help JavaScript become more popular and attract more developers to use JavaScript as their primary language for development. This new standard includes the biggest set of reforms JavaScript received since the language was created. If I were a new developer or starting my freelance developer career, I would invest time in learning JavaScript now, because in about 2 years the amount of work for JavaScript developers could be significantly bigger than for other platforms.

Postado 30 junho, 2015

Greg Bogdan

Software Engineer, Blogger, Tech Enthusiast

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript. I like technical writing and have good experience in creating tutorials and how to technical articles. I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...

Próximo Artigo

Why This Fresh Grad Turned Down a Top Tech Job