Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. var a = 20;
  2.  
  3. function myFunc1() {
  4. console.log(a); //20
  5. }
  6.  
  7. function myFunc2() {
  8. console.log(a); //undefined
  9. var a = 21;
  10. }
  11.  
  12. myFunc1();
  13. myFunc2();
  14.  
  15. //Notice the difference in 2 console.logs
  16. //The 1st one is 20 because of lexical scope chain (myFunc1 can gets the scope of global execution context object)
  17. //The 2nd one is undefined because of line "var a = 21". This creates a variable 'a' with undefined value in VO of myFunc2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement