2

In following examle, how in world does first window.object (console.log first time) contain variable a that too with value '10'? As, there is no global function wrapping so concept of 'hoisting' does not occur, even if it occurs - a will be hoisted as value undefined.

I can understand second window object has property a as '10', but how can first window object before declaring and putting variable value as '10' still containing it. What is happening?

Note: 'StackOverflow' wraps the code in its own global function, so not running here.

console.log(this);

var a = 10;

console.log(this);

Outcome:

enter image description here enter image description here

Note: This code runs in 'chrome'.

Deadpool
  • 7,811
  • 9
  • 44
  • 88
  • Is this on chrome or firefox? – EKW Mar 30 '18 at 08:01
  • The code runs in 'chrome'. – Deadpool Mar 30 '18 at 08:03
  • 2
    as far as i can make out is while logging the objects are logged fine but. In console when you expand them for the first time they are evaluated the window object already have a reference to `a` so both the logs show it. instead try this log the first statement then check the logged object it wont have a. Then declare the variable and then log the object again now will see a in second log. And a wont be there in first log. – Manish Mar 30 '18 at 08:04
  • The property exists because it is hoisted. It has its value logged because the console expands the object lazily. – deceze Mar 30 '18 at 08:12

0 Answers0