Scope of variables in JavaScript

1) Global Scope

2) Local scope

3) Block Scope

a) No block scope in JavaScript till ES6.

b) ES6 introduced let

4) Intermediate: Object properties

5) AdvancedClosure

6) AdvancedPrototype-based scope resolution

7) Global+LocalAn extra complex Case

This will print out undefined and 10 rather than 5 and 10 since JavaScript always moves variable declarations (not initializations) to the top of the scope, making the code equivalent to:

8) Catch clause-scoped variable

This will print out 565. Inside the catch clause e shadows global and local variables. But this special scope is only for the caught variable. If you write var f; inside the catch clause, then it’s exactly the same as if you had defined it before or after the try-catch block

 

Leave a Reply

Your email address will not be published. Required fields are marked *