Some cool features you should know about ES6 in javaScript
javaScript has become more simpler after the es6 function is introduced in javaScript. Es6 makes our life easier. Here I’m writing about some cool features in ES6.
- Arrow function : Arrow function is one of the best features introduced in ES6. You can create a function in cleaner way than before. If the return statement in a single line then we can return everything in a single statement.
2. Class : Classes are a template of creating objects. This helps to create the object oriented programming concept implemented in javaScript. Before ES6 we have to write those code inside function but after introducing es6 we can write this like,
3. Template literals : It is one of the coolest feature of ES6. It helps user to dynamically set value inside the JSX element. Previously we have to concat multiple strings and make a new string. But introducing template literals, we can do this simpl using back ticks. Example is bellow:
4. Function with default Parameter : This is a valueable feature in es6. From now on, we can add default parameter into a function. Previously we have to set it inside the function with the this keyword. This makes code way more cleaner than before. Example:
5. Spread Operator : Spread operator is commonly used to take the shallow copy of javaScript object and array. Using this operator makes the code concise and enhances its readability. It is widely used to concatenating or combining arrays.
6. Rest Parameter : Can be used in two different ways. One is spread parameter and other is rest parameter. Rest parameter converts all elements into an array. It allows us to pass infinite numbers of argument pass to a function and read them through an array, Before ES6 arguments variable is used into a function to work with this.
7. Object Destructuring : It is used to unpack an array or object properties into distinct variables. This cool feature comes with ES6. we can extract data from arrays and objects and assign them to variables. Object destructuring can extract multiple properties in one statement, can access properties from nested objects, and can set a default value if the property doesn’t exist.
8. Variable declaring : const and let are two new variables that introduced in ES6. It is used to reduce the memory leakage. const is means constant that we cannot change the value of it where if we want to change the value of any variable we can use let.
9. Hoisting : Hoisting is the default behavior of moving all the declarations at the top of the scope before code execution. For hoisting a variable can be used before it is declared. Basically, it gives us an advantage that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.
10. Scope : There are two kinds of scope in javaScript. One is local scope and other one is global scope. In the local scope, variable are defined into an curly brace and in global scope the variable is defined globally and everywhere we can access it. Local scope only can access inside the curly braces.