Javascript ES6 Learn From Scratch - Ega Prasetya
Tags: javascript, tutorial
Published: 2020-05-30 08:45:57

JavaScript ES6 brings new syntax and new awesome features to make your code more modern and more readable. It allows you to write less code and do more. ES6 introduces us to many great features like arrow functions, template strings, class destruction, Modules… and more. Let’s take a look.

# CONST and LET

Const is a new keyword in ES6 for declaring variables. const is more powerful than var. Once used, the variable can’t be reassigned. In other words, it’s an immutable variable except when it 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 don’t want to reassign the variable .

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e9s6z1iz2qkvkbeccmzg.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2uu0bgoauscz2o1xwl93.png)

let can be reassigned and take new value. It creates a mutable variable.
let is the same as const in that both are blocked-scope. It means that the variable is only available within its scope.

# Arrow Functions

The arrow function is really awesome, and makes your code more readable, more structured, and look like modern code. Instead of using this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bay2y3m50pcqicrt7jtp.png)

Use this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nm7sr0c6hjzkvjwvxy3i.png)

As you see, the arrow function seems more readable and clean! You won’t need to use the old syntax anymore.
Also, you can use Arrow function with map, filter, and reduce built-in functions.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ccy9tn98k1fi43hsiyja.png)

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:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v707abyp9shjt7cnq4gb.png)

With new ES6 syntax:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jwoipcl11eijuq3ybt6s.png)

So simple! It’s a really huge difference between the old syntax and ES6. When playing with strings, the literal string in ES6 looks more organized and well structured 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:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w05devqsl25je6fgbdst.png)

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!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z1hblo67jxd4ncikqqzo.png)

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:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r5am2v1tdfj8km4oalb8.png)

With ES6 syntax:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mno3rdat7fqbw2hwzjvg.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rbq3xo2071yi3x3uzo82.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rmkeszdrntsrb6p5xmzz.png)

# 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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g5w6ppglnzzprm4hef2i.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9b7kyifhxxxcd9insrub.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b8df6y6v4m693dr9dg8v.png)

So cool, isn’t it?!

# Promises

Promises are a new feature of ES6. It’s a method to write asynchronous code. It can be used when, for example, we want to fetch data from an API, or when we have a function that takes time to be executed. Promises make it easier to solve the problem, so let’s create our first Promise!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/338914yoqrlv9u1bhon5.png)

If you log your console, it will return a Promise. So, if we want to execute a function after data is fetched, we will use a Promise. The Promise takes two parameters: resolve and reject to handle an expected error.

# Rest parameter and Spread operator

The rest parameters are used to get the argument of an array, and return a new array.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/clj7y57ydu0cwswyov1f.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jwaabx48ia0j3pss2meu.png)

The spread operator has the same syntax as the rest parameter, but the spread operator takes the Array itself and not just the arguments. We can use the Spread parameter to get the values of an Array, instead of using a for loop or any other method.

# Classes

Classes are the core of object oriented programming (OOP). They make your code more secure and encapsulated. Using classes gives your code a nice structure and keeps it oriented.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/of36x953sv6rszi0ifmc.png)

To create a class, use the class keyword followed by the name of the class with two curly brackets.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/if4tr3xlqfbi0q95l3wc.png)

Now we can access the class methods and properties using the new keyword.
To inherit from another class, use the extends keyword followed by the name of the class you want to inherit from.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i6r6nuev96p79qitp03z.png)

You can learn more about Classes here.

Okay, thx for read the docs hope your enjoy it.