1

In JavaScript when we define a new variable like this var a = true; in the global scope this variable becomes a global object property like window in the browser so we can reach it by window.a;, same for any object or array properties myObj.property; and myArr[0]; for example.

Even the function itself as an object we can define a properties inside the function object function foo(){}; foo.prop = value;.

But when we declare a variable inside a function using var or let like in this case

function foo(){
    var v = 123;
}

This v variable is a property of what ? i thought it might be defined as non enumerable property inside of the function object it self or the constructor but when i use Object.getOwnPropertyNames() i can't find them, So where these variables supposed to be properties ? or they are not supposed to be and they are just variables in the memory without being properties for any object ?

0

4 Answers 4

2

The fact that declaring a variable in the global scope (ie: var a = 'hello') and it being made accessible through the global object (the window in the case of the browser) is not "normal" behaviour when declaring variables.

"or they are not supposed to be and they are just variables in the memory without being properties for any object ?" - this is correct.

With that being said, when not using strict mode (ie: by putting 'use strict' right at the top of your script), if you declare a variable inside of a function without var (or let etc) in front of the variable name, it will also become accessible through the global object. For example:

 function setGlobal() {
   shouldNotBeGlobal = 'but it is!';
 } 
 setGlobal();

 console.log(window.shouldNotBeGlobal)  // "but it is!"

The above is possible because the JavaScript interpreter looks through all layers of scope, accessible to the function, trying to find where shouldNotBeGlobal was declared so as to set a new value of "but it is!". However it never finds that variable and then decides to be kind and create it for you within the global context.

This will not work:

 function setGlobal() {
   var shouldNotBeGlobal = 'but it is!';
 } 
 setGlobal();

 console.log(window.shouldNotBeGlobal)  // "undefined"

Generally speaking, when declaring a variable within a function, a new execution context is created, and so variables created within that scope are only available within that scope. They do not become "part" of any object.

Sign up to request clarification or add additional context in comments.

1 Comment

"and then decides to be kind and..." Don't anthropomorphize the JavaScript runtime. Consider the the behavior of window when not in strict mode. The global scope is essentially a giant with (window) { ... } wrapping the entire source, so an implicit declaration will attach itself to the window scope when it finds no declaration in any of the more immediate scopes.
1

When the function is invoked, a new context starts. The variables defined in the function scope are part of that context. They are not accessible outside the function, unless the function returns some object referencing that variable.

The inaccessibility of these variables is a good thing as it avoids polluting the global space.

3 Comments

Although it is a valid question, you’d be better off properly reading through the basics of a language before using it. Read a good guide on js that explains scope, objects, hoisting etc.
@ChrisCousins did you mean to leave that comment on the question? This answer is technically accurate.
Yes although the answer is accurate, the question shows a lack of basic understanding of the language- it is therefore better advice to go read the basics, rather than getting spot and sparse answers to specifics.
0

This v variable is a property of what ?

simple you created private variable v , it is not property of foo

when ever you write foo.v = 123 you are creating a static property of foo

Comments

0

Actually v is an identifier, which evaluates to a Reference with the base being an EnvironmentRecord, and that is only created when invoking a function, and can only be accessed inside the function itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.