JavaScript variable with global scope [closed]

The following will happen:

An alert popping up, displaying the value of aa = 10

An alert popping up, saying undefined since you are trying to access a variable x from the global scope, however x is only defined in the scope of function a.

An error in your console, ReferenceError: x is not defined.

So, as you assume, x indeed is private, you can’t access it globally.

You probably messed something up giving you wrong results.

What might have been the case is that you forgot the var in front of the x which suddenly makes it a member of the global object instead of being restricted to the function-scope. In this case, the last alert would give you 13. However the alert(typeof x) would give you "number" then.

Leave a Comment