JavaScript Ecma Script-6 important topic for beginners
Here I am going to discuss Ecma script-6's most important parts to beginners. Here we go…
Block-Bindings:
we declare variable all kinds of programming language such as c,c++, java, and javascript also. In javascript, we declare a variable in many ways. Before Es-6 came, we used var to declare a variable. Block means it can not access outside the function or where it is declared. Declare a variable Like =>
var sum=0;
But when ES-6 came, we declare a variable in let and const.
let sum=0;
const sum = 0;
let means this value can be changed but const means this is can not be changed.
Var declaration and hoisting:
When we declare a variable using var and it is top of the function and where the actual declaration occurs this is called hoisting. Example =>
function sum (a, b){
var total = 0;
total = a + b;
return total;}
Block Binding in loop:
Example:
for( var i = 0; i≤5; i++){
var element = element[i];
console.log(element);}
let and const declaretion in loop:
In ES-6 we can use let and changed var. Like =>
for( let i = 0; i≤5; i++){
let element = element[i];
console.log(element);}
Function in ES-6:
When ES-6 has come, the Whole function declare pattern changed, and before we declare function it was like =>
function sum (a, b){
var total = 0;
total = a + b;
return total;}
But in Es-6 we can use the arrow function to declare a function.it like =>
let sum = (a, b) => a + b;
Rest parameters:
It is declared by three dots and it has two restrictions and one is there can be only one rest parameter, and the rest parameter must be last.
function checkArguments(…args){// your code
}
The spread operator:
It is the closely same of rest parameters. the spread operator allows you to specify an array that should be split and have its items passed in as separate arguments to a function. we can use Math. max for an example =>
let a = 10,b = 15;
console.log(Math.max(a,b));
Ans is 15;
Block-Level Functions:
“use-strict”;
if(true){
console.log(‘block’);
function test(){
console.log(‘test’);
}
Arrow function :
It is more efficient and easy to use example =>
let sum=(a,b)=>a+b;
In Array, we can use arrow function in many ways like =>
const number=[1,2,3,4,5];
console.log(number.map(num => number.length));\
Tail call optimization:
A tail call is when a function is called as the last statement in another function, like this =>
function doSomething(){
return doSomethingElse();}//tail call
How to Harness Tail Call Optimization:
It happens when we using recursive. Factorial example given below =>
function factorial(n){
if(n ≤1){
return 1;
}
else {
return n* factorial (n-1);
}
}
this is not an optimization code , because multiplication must happen after the recursive call to factorial().Optimazation process =>
function factorial(n, p=1){
if(n ≤1){
return 1*p;
}
else {
let result = n * p;
return factorial (n-1,result);
}
}
That's all. I hope it will helpful for beginners. Thank you for reading.