JavaScript ES6….

Kamrul Hasan
2 min readMay 6, 2021
  1. Block bindings occur whenever we declare or set a value in a variable.ES6 offers block bindings using var, let, and const. Example: var = 1; let = 2; const = 3
  2. Global Block Bindings Global scope behavior for var is different than let and const. When we use var in the global scope a new global variable is created and its property on the global object but if we use let or const in the global scope a new binding is created in the global scope but no property created on the global scope. For example var sample = ‘Hello world’; console.log(sample) // Hello world; var name = ‘Karim; console.log(window.name); // Karim; When we use let: var sample = ‘Hello world’; console.log(sample); // Hello world; console.log(window.sample == sample);// false.
  3. Block Bindings in Loop block level is very useful when dealing with loops in javascript. Example for (var i = 0; i < 20; i++){//some code; console.log(i);//20}
  4. Function with default parameter values In ES5 and earlier requires lots of extra code to simulate default parameter value but ES6 makes it easier to pass default parameters when the parameter is not formally passed when calling the function. For example function add(num1,num2=0){//here 0 is the default parameter value return num1 + num2} console.log(add(20,5))//25; console.log(add(20))//20
  5. Working with unnamed parameters ES6 introduces the rest parameter to work with unnamed parameters. The rest parameter allows any number of arguments as an array. Example function(..args){return args.reduce((accumulator, current) =>accumulator + current,0)}console.log(add(5))// 5; console.log(add(5,10))// 15; console.log(add(5,5,10))// 20;
  6. Block-Level Function ES6 allows block-level functions which are hoisted on top of the function or hoisted into the global scope. Example if (true){console.log(typeof doMath)// “function” function doMath(){//some code} doMath()} console.log(typeof doMath) // function
  7. Arrow functions ES6 allows the alternative way to write shorter format named arrow function compared to traditional function expression. Example let add = (a,b) => a+b; console.log(add(7,5))//12

8.Error handling try…catch The try…catch syntax is try{//code } catch(err){//error handling} It works like this: the code in try is executed. if there were no errors then catch is ignored. If an error occurs then the try execution is stopped and control flows to the beginning of catch. The error variable will contain an error object with details about what happened.

9.Var declaration and hoisting when we declare a variabe with var they are hoisted to the top of the function or global scope if declared outside a function. Example function getName(condition){if (condition){var name = ‘Rahim’; return name}else{return ‘color not found’}}/////////////////////// function getName(condition){var name if(conditon) { name = ‘Rahim’ return name}else{return ‘color not found’}}

10.Emerging Best Practices For Block Bindings during ES6 development the convention was to use let instead of var and use const to limit the modification but now more developers are migrated. Now developers are following a convention that used const as the default and used let when you need to change the value.

--

--