Declare a variable without the var keyword and logical OR

I get weird behavior when declaring an object with a logical OR.

my_var = my_var || {}; // throws TypeError 

If I add the var keyword

 var my_var = my_var || {}; // returns empty object 

Why is this? I can't seem to find an explanation. my_var is a global scope, so why does var change behavior?

+8
javascript
source share
7 answers

The first example tries to assign a property to a global object named my_var by reading a value from an identifier named my_var (OR an empty object). However, the identifier my_var not defined at this point, so it fails.

In the second example, because of how the javascript variable works, the variable my_var already declared when you read it, assigning it to it.

Also look at this example:

 a = a; // fails, undeclared identifier a = 0; 

It will work with the var keyword!

 b = b; // succeeds allthough identifier undeclared?! var b = 0; 

This is due to the fact that a variable lifting device will turn it into this:

 var b; // declaration of b hoisted to the top of scope b = b; b = 0; 
+5
source share

In your second example, var defines the scope of my_var . This allows you to set the value correctly. Here's a quick and dirty look at what happens in both examples:

In Example 1, the JS engine should do the following:

  • Check the current scope if my_var .
  • Check the scope above (and the scope above) until it finds my_var .
  • Create my_var in the global scope if it is not found. *

* Step 3 has not yet occurred when you try to assign the value my_var .

Example 2:

  • Is there a variable called my_var in the current scope? Yes, you just created it using the var keyword.
  • Assign its value.
+1
source share

when defining a variable without var you get direct access to the global object . This means that your Javascript engine is trying to find my_var for the global object ( window if you are in a browser). Since this property does not exist yet, your JS engine will throw.

This happens on the right side of your statement when your engine tries to read a variable called my_var . appointment as

 my_var = {}; 

will work. But accessing the identifier without var will cause the browser to search for the scopechain object. Since the variable object for the global object is the global object itself, the search procedure will not be anywhere (= exception).

By placing the var infront keyword, your js engine knows in the paragenem that it must declare a new variable with this identifier name. It actually declares this variable with an undefined value (called "raise"). This means that using var will prematurely create a property in the current context with this name. Therefore, if this code is not in any function or in the context of eval, it will create the my_var property of the window object.

+1
source share

In the second situation, my var is undefined, so the variable is assigned an empty object, which {}

0
source share

Although I cannot quote the specifications, I assume that the initialization in the var statement is just syntactic sugar:

 var my_var = my_var || {}; 

equivalent to:

 var my_var; my_var = my_var || {}; 

In the first line, my_var declared and implicitly set to the special value undefined . Then in the second line my_var exists, undefined evaluates to false and no error occurs.

In your first example, however, my_var not known when the right side is evaluated, hence the error.

0
source share

In the first case, you are trying to assign a variable that has not been defined. Its the same as you would write:

 my_var = a || {}; 

in the second case, my_var is only undefined, since you are using the var keyword, create my_var in the actual scope. undefined is converted to false and returns an empty object.

0
source share

Interestingly, this will work:

 a = my_var || {}; // 'a' is now an empty object 

When you use the keyword 'var', you literally give the command "create a new variable and assign it that value." If you do not use "var", you mean that the variable already exists within the global scope somewhere.

In your example, the JavaScript engine tries to find the current value of "my_var", detects that it does not exist, cannot create it, because you are currently trying to assign a value to it and it fails.

0
source share

All Articles