Do you know that JavaScript is an application which is used for a general-purpose client-side scripting language requirement called ECMAScript? We can also say that ECMAScript (or formally ECMA-262) is the standard that describes exactly how we apply JavaScript, as well as what we can accomplish with it. To understand it in depth it is recommended that individuals should join JavaScript courses in Delhi from reputed institutes.
This blog will certainly provide you a quick intro to ES6. If you don’t understand what ES6 is, it’s a new JavaScript implementation. If you’re a busy software engineer, then thoroughly read this blog to discover the best 10 features of the new generation of one of the most prominent programming language– JavaScript.
New Features of ECMAScript or ES6 that Every JavaScript Developer Must Know
1. New Rest Parameters and Default Values for Parameters
In ES6, we can add default values to the parameters of a function. This implies that the default parameters will certainly be used if we do not pass in arguments later in the function call. In the case of ES5, default values of parameters are always set to undefined. Therefore, the new possibility to set them to whatever we want is a terrific improvement which is done in the language.
function sum(a = 2, b = 4) {
return a + b;
}
console.log( sum() ); // 6
console.log( sum(3, 6) ); // 9
ES6 has also added a new type of parameter which is known as the rest parameters. They work in the same way to the spread operators. They are also useful if we don’t know exactly how many arguments in the function call will be passed later on. We can make use of the properties and methods of the Array object on rest parameters.
function putInAlphabet(...args){
let sorted = args.sort();
return sorted;
}
console.log( putInAlphabet("e","c","m","a","s","c","r","i","p","t") );
// a,c,c,e,i,m,p,r,s,t
2. New const Keyword
With the help of this keyword, we can declare constants. They are also known as immutable variables, which cannot be reassigned later on.
const MY_CONST = 12;
console.log(MY_CONST); // 12
MY_CONST = 16;
// Silent error, as we cannot reassign a new value to a constant
3. New let Keyword
It will be used to declare local variables in the scope of a block. It can be an expression, a statement, or an (n inner) function. For example, we can declare a for loop in the following is shown example and after that reuse, the same variable name inside the next if statement:
for (let i = 0; i< myArray.length; i++) {
// Do something inside the block
}
if (x > 0 && x != y) {
// We reuse "i"
let i = x * y;
}
The distinction in between let and far remains in the scope. For example, a local variable defined by the var keywords can be utilized in the whole enclosing function, however variables specified by let only work in their very own (sub)block. Let can similarly be utilized globally; in this instance, it acts similarly as var. Certainly, in ES6 we can still make use of var if we desire.
4. Arrow functions
ECMAScript 6 approves entirely leaving out the function keyword and just how we comprise anonymous functions, and. We only require to utilize the new syntax structure for arrow functions, called after the => arrow sign that (fat arrow), that offers us a wonderful shortcut.
// 1. Parameter in ES6
let sum = (a, b) => a + b;
// in ES5
var sum = function(a, b) {
return a + b;
}
// 2. Without parameters in ES6
let random = () => Math.random();
// in ES5
var randomNum = function() {
return Math.random();
}
// 3. Without return in ES6
let message = (name) => alert("Hi ") + name + "!");
// in ES5
var message = function(yourName) {
alert("Hi " + yourName + "!");
}
There’s a crucial distinction in between regular and also arrow features, that is arrow features do not obtain this value immediately like functions defined with the function keyword phrase do. Arrow functions operate lexically and bind this value to the present scope. This implies that we can quickly recycle this keyword in an inner function.
5. New spread Operator
It is a type of operator which is represented with 3 dots (…) and can be utilized to sign numerous places expected items. One of the utmost common uses of the spread operator is simply inserting the elements of an array into another array:
let myArray = [1, 2, 3];
let newArray = [...myArray, 4, 5, 6];
console.log(newArray);
//1, 2, 3, 4, 5, 6
The spread operator is rather flexible, as it can be used numerous times in the same array or function call.
6. New for…of Statement
Using the new for … of the loop, we can repeat over arrays or various other iterable objects quickly. Together with the new for … of the statement, ECMAScript 6 introduces 2 new iterable objects also, Map for key/value maps, and Set for collections of distinct values that can additionally be primitive values and also object references. When we utilize the for … of the statement, the code inside the block is executed for every element of the iterable objects.
let myArray = [1, 2, 3, 4, 5];
let sum = 0;
for (let i of myArray) {
sum += i;
}
console.log(sum);
// 15 (= 1 + 2 + 3 + 4 +5)
7. Classes
ES6 has JavaScript classes that are constructed on the existing prototype-based inheritance. The new syntax structure makes it extra simple to create objects, take utilize of inheritance, and also reuse code. It will certainly likewise make it less complicated for novices getting here from various other programs languages to recognize exactly how JavaScript functions.
You can declare the classes using the new class keyword in ES6. It also requires to have a constructor () method that is called when a new object is instantiated by using the new myClass() syntax. It’s additionally feasible to expand new classes with the class Child extends Parent syntax that can be familiar from other object-oriented various other languages such as PHP. It’s additionally vital to recognize that, unlike function and variable declarations are NOT hoisted in ECMAScript 6.
8. Template Literals
ECMAScript 6 provides us with a new option for string concatenation. Template literals permit us to conveniently produce layouts in which we can embed various values to any place we desire. To do so, we require to utilize the ${…} syntax anywhere where we intend to insert the data that we can pass in from variables, arrays, or objects in the list below means:
let customer = { title: 'Ms', firstname: 'Jane', surname: 'Doe!', age: 34};
let template = `Dear ${customer.title} ${customer.firstname} Happy ${customer.age}th birthday!`;
console.log(template);
// Dear Ms Jane Doe! Happy 34th birthday!
9. Modules
Have you ever thought about how cool it would be if JavaScript is modular? Naturally, there have been workarounds such as CommonJS (utilized in Node.js) or AMD (Asynchronous Module Definition) (made use of in RequireJS) to do that before. However, ES6 presents modules as a native feature.
We need to specify each module in its file, then with the help of the export keyword, export variables and functions to numerous other files, and the import keyword is used to import them from numerous other files, as mentioned in the following syntax:
// functions.js
function cube(a) {
return a * a * a;
}
function cubeRoot() {
return Math.cbrt(a);
}
export { cube, cubeRoot };
// or: export { cube as cb, cubeRoot as cr }
// app.js
import { cube, cubeRoot } from 'functions';
console.log(cube(4));
// 64
console.log(cubeRoot(125));
// 5
10. Loads of New Methods
ECMAScript 6 presents several new methods for the existing String Prototype, Array Prototype, Array Object, and also Math Object. The new methods can substantially boost the process of exactly how we can control these entities. Mozilla Dev has wonderful code instances of the new additions; it worth to take some time as well as extensively analyse them.
function isPrime(element, index, array) {
var start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) {
return false;
}
}
return element > 1;
}
console.log([4, 6, 8, 12].find(isPrime));
// undefined, not found
console.log([4, 5, 8, 12].find(isPrime));
// 5
If you’re a programmer, you would be most likely to learn all the features discussed above regarding ECMAScript 6 (ES6). The best way to learn them is by joining a professional JavaScript training institute in Delhi where you get practical exposure to all the above features. All the existing browsers have complete assistance to ES6. Even if you are targeting IE11, heritage internet browsers, you can still utilize ES6 with the outstanding babel compiler option available. It is called a “compiler” due to the fact that it changes ES6 code right into ES5 code to assure that as long as your net web browser can stand up to ES5, you can take advantage of ES6 code securely.