Author: Aleksandar Mihajlovski, Developer
ECMAScript 6, also known as ECMAScript 2015, is the latest version of the ECMAScript standard. ES6 is a significant update to the language, and the first update to the language since ES5 was standardized in 2009.
- Block scoping
- Arrow functions
New way of defining variables
Let it be, let it be…
ECMAScript 6 introduces a new keyword to declare variables: let. Unlike variables declared with var that are function-scoped, variables declared with let are block-scoped, they only exist in the block they are defined in. If we try to use that variable out of the specific block where it is defined (out of the scope), then the application will throw reference type error.
Notice that when I’m invoking the method without any argument provided to the function, it returns undefined value instead of throwing an error. That’s because the if statement will never execute, so the result variable won’t be declared and will remain undefined.
An arrow function expression has a shorter syntax compared to function expressions and lexically binds the this value. Arrow functions are always anonymous.
This keyword refers to the object where it is called, but what about if the this keyword was inside function? In that case, this keyword refers to the owner of the function it is in, so it means that this belongs to the window object.
The comparison between functions in ES5 and ES6 would be the following:
Classes are in fact special functions, and just as you can define function expressions and function declarations, the class syntax has two components:
One way to define a class is using a class declaration. To declare a class, you use the class keyword with the name of the class.
A class expression is another way to define a class. Class expressions can be named or unnamed. The name given to a named class expression is local to the class’s body.
Current support for ES6 is quite limited and a lot of the ES6 specification is still in draft form. We can look forward to these features (and much more) in the near future.