skip to Main Content

snapshot of code inside chrome dev tools debugger

As seen from the above snapshot, the variable "age" exists in the local scope but outside the window object. Why does it happen? Is there any in-depth explanation to it.

I checked the values and scope state using chrome dev tools debugger.

2

Answers


  1. Local variables do not show up as window properties. Local variables are variables defined in the scope of functions. On the other hand, global variables are available as properties of window.

    Login or Signup to reply.
  2. Variables declared under function have function scope only.

    function myFunction() {
    var localVar = "I am a local variable";
    console.log(localVar); } // This will log: "I am a local variable
    
    console.log(typeof localVar); // This will log: "undefined"
    

    But, Variables declared with var at the global scope, however, do become properties of the window object in browsers, but this behavior doesn’t apply to variables declared inside function scopes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search