10 things you don’t know of ES6

Md. Abul Hasan Nur Rafi
2 min readMay 6, 2021

1. Var Hosting

In a JavaScript code, var declarations are processed before the code executed. If we declare any variable anywhere in code or scope Javascript declare it at the top. This behaviour is called hosting.

function checking_hosting() {
console.log(num); // undefined
var num = 23;
console.log(num); // 23
}

// ...is implicitly understood as:

function checking_hosting() {
var num;
console.log(num); // undefined
num = 23;
console.log(num); // 23
}

2. Block-Level Declarations

The block-level declaration means those variables which are declared in scope & which is not accessible outside of the scope. You can create a Block-scope inside of a function, inside of a block means code with a bracket {}

3. Block Binding in Loops

Developers want to code with a block scoping in for loop, if we use var in for loop then the variable is accessible outside of the for loop, on the other hand, if we use let instead of var this let is not accessible outside of the for looop.

for (var i=0; i < 10; i++) {
process(items[i]);
}

// i is still accessible here
console.log(i);
for (let i=0; i < 10; i++) {
process(items[i]);
}

// i is not accessible here - throws an error
console.log(i)

4. Global Block Bindings

let and const are different from their global scope. var create a global object in the browser. That's mean we can accidentally overwrite a variable. Instead of var if we use let or const a binding is created in the global scope but no global object is created

5. Emerging Best Practices for Block Bindings

Before es6 javascript developer used the only var. after publishing es6 let and const came with great features of scoping we should use let for those variables which might be changed & const should use for default value because unexpected values are a source of bug

6.

--

--

Md. Abul Hasan Nur Rafi
0 Followers

I am a high voltage powered react js web developer