JavaScript ES6 brings us new syntax and some other features to make our life easy. It allows you to write less code and make more. ES6 gives us the ability to provide many features. Strings, class destructions, Modules, arrow functions. Let’s dive in and go for deeper understanding.

const and let

const is a new keyword in ES6 for declaring variables. Const has some difference than var. When we assign anything with const we cant re-assign again. Its immutable variable when it is used with objects.

This is really useful for targeting the selectors. For example, when we have a single button that fires an event, or when you want to select an HTML element in JavaScript, use const instead of var. This is because var is ‘hoisted’. It’s always preferable to use const when you don’t want to reassign the variable .

In the code above, const will not change and cannot be reassigned. If you try to give it a new value, it will return you an error.

If you remember what I said about const that when you assign any value you can’t change it. But it is different for let. If you assign any value the variable which you declared with let you can re-assign it again. It makes a mutable variable.

let and const has some mutual features. They are both blocked-scope. Its means that the variable is only available within its scope.

Arrow functions

The arrow function is the most extreme change in JavaScript. It’s very convenient when you need to implement a callback function. It also looks modern and unique. Let’s see the two functions with arrow and without arrow function.

With Arrow Function

const myFunc = name

As you see, the arrow function seems very handy , readable and clean! We don’t need to use ES5 anymore.

Also, you can use Arrow function with map, filter, and reduce built-in functions.

The map function with arrows looks more clear and readable than map in ES5. With ES6 you can write shorter and smarter code. You can use the same with filter and reduce.

Template Literals

Template literals or template strings are pretty cool. We don’t have to use the plus (+) operator to concatenate strings, or when we want to use a variable inside a string.

The old syntax:

With new ES6 syntax:

As you see its also big difference in syntax when you create a template with ES6. When we are manipulating in array which has strings with ES6, string look more organized and looks better than ES5.

Default parameters

When I work in PHP, I usually use default parameters. These allow you to define a parameter in advance.

So, when you forget to write the parameter, it won’t return an undefined error because the parameter is already defined in the default. So when you run your function with a missed parameter, it will take the value of the default parameter t, and it will not return an error!

Look at this example:

The function above returns undefined, because we forgot to give it the second parameter age.

But if we used the default parameter, it won’t return undefined, and it will use its value when we forget to assign a parameter!

As you see, the function returns a value even though we missed the second parameter. Now with the default parameter we can handle the error in advance.

Array and object destructing

Destruction makes the assignment of the values of an array or object to the new variable easier.

The old syntax:

With ES6 syntax:

With ES5, we have to assign each value to each variable. With ES6, we just put our values within curly brackets to get any property of the object.

Note: if you assign a variable that is not identical to the name of property, it will return undefined. For example, if the name of the property is name and we assign it to a username variable, it will return undefined.

We always have to name the variable the same as the name of the property. But in case we want to rename the variable, we can use the colon : instead.

For the array, we use the same syntax as the object. We have just to replace the curly brackets with square brackets.

Import and export

Using import and export in your JavaScript application makes it more powerful. They allow you to create separate and reusable components.

If you are familiar with any JavaScript MVC framework, you will see that they use import and export to handle the components most of the time. So how do they really work?

It is simple! export allows you to export a module to be used in another JavaScript component. We use import to import that module to use it in our component.

For example, we have two files. The first is named detailComponent.js and the second is named homeComponent.js.

In detailComponent.js we are going to export the detail function.

And if we want to use this function in homeComponent.js, we will just use import.

If we want to import more than one module, we just put them within curly brackets.

Software Engineer | Ruby on Rails | JavaScript | HTML5 | CSS | PostgreSQL | React | Redux