Block Binding

NayemulAlam
2 min readNov 7, 2020

we will know about block binding

Var declaration and Hoisting

There are two types of variable in JavaScript, Global and Local. We can access global variable from anywhere but local variable is accessible from only that’s function.

1. var globalAnimal = "Cow";
2.
3. function animal(){
4. var localAnimal = "Elephent";
5. console.log(globalAnimal);
6. console.log(localAnimal);
7. }
8.
9. console.log(globalAnimal);
10. console.log(localAnimal);
//output
line5: Cow
line6: Elephent
line9: Cow
line10: Error
//

Hoisting : When we create a function and show a output before declaring a variable the output will show us undefined and it’s call Hoisting. Before declare a variable we can’t access it’s value and output would be undefined.

function hoisting() {
console.log(color);
var color= "Red";
}
hoisting();
// output: undefined //
function hoisting() {
var color= "Red";
console.log(color);
}
hoisting();
// output: Red //

Block Level Declaration

When we declare let or const variable we could not access them from outside of the block scope. There are two types of scopes in JavaScript function block and inside a block in {} . When we declare loaclVar we can access the var in only the fucntion , and when we declare name var we can access the var only inside the block { }

1. function blockDec() {2. let localVar = "Nayemul";3. if (true) {4. let name = 'sifat';5. console.log(name);
6. console.log(localVar);
// output: sifat;
Nayemul;
}
7. if (true) {
8. console.log(localVar);
9. console.log(name);
//output: Nayemul
error
}
}
10. blockDec()

Block Binding in loops

It also look like the Block Level Declaration but it’s work only in loop. when we use var we can access the value out of the loop but when we use let we can’t access the value out of the loop.

for (var i = 0; i < 10; i++) {
console.log(i);
}
console.log(i);
// we can access the value of i out of the loop
for (let t =0; t < 10; t++) {
console.log(t);
}

console.log(t);
// we can't use the value of t out of the loop

Global Binding

Global block binding is normally when we declare/initialize a variable outside of function or in a curly bracket{} we can access them from anywhere in program .

Emerging Best Practices for Block Bindings

While ES6 was in development, there was widespread belief you should use let by default instead of var for variable declarations. For many JavaScript developers, let behaves exactly the way they thought var should have behaved, and so the direct replacement makes logical sense. In this case, you would use const for variables that needed modification protection.

However, as more developers migrated to ECMAScript 6, an alternate approach gained popularity: use const by default and only use let when you know a variable's value needs to change. The rationale is that most variables should not change their value after initialization because unexpected value changes are a source of bugs. This idea has a significant amount of traction and is worth exploring in your code as you adopt ES 6.

--

--