I get a strange behavior when declaring an object with the 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 global scope, so why is var
changing the behavior?
If you declare a variable, without using "var", the variable always becomes GLOBAL.
let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.
In the old, pre-ES6 era of JavaScript, developers used to declare variables using the keyword var or without any keywords. But times have changed! With ES6 (EcmaScript 2015), the beginning of the modern era in JavaScript, the language got two new keywords to help us declare variables. These are let and const .
Value = undefined In computer programs, variables are often declared without a value. The value can be something that has to be calculated, or something that will be provided later, like user input. A variable declared without a value will have the value undefined .
The first example tries to assign to a property on the global object named my_var
by reading the value from an identifier called my_var
(OR an empty object). However, the identifier my_var
is not defined at that point, so it fails.
In the second example, due to how javascript variable hoisting works, the my_var
variable is already declared, when you read from it by assign to it.
Also have a look at this example:
a = a; // fails, undeclared identifier
a = 0;
With var keyword it will work!
b = b; // succeeds allthough identifier undeclared?!
var b = 0;
This is because variable hoisting will turn it into this:
var b; // declaration of b hoisted to the top of scope
b = b;
b = 0;
when defining a variable without var
, you're directly accessing the global object
. That means, your Javascript engine trys to lookup my_var
on the global object (window
if you're in a browser). Since that property does not exist yet, your JS engine will throw.
That happens on the right side of your statement, when your engine trys to read a variable with the name my_var
. assigning like
my_var = {};
would work tho. But accessing an identifier without var
will cause the browser to lookup the scopechain. Since the variable object
for the global object is the global object itself, the lookup procedure will end up nowhere ( = exception ).
By putting the var
keyword infront, your js engine knows at parsetime that it has to declare a new variable with that identifier name. It actually does declare that variable with an undefined
value (that is called "hoisting"). That means, using var
will prematurely create a property in the current context
with that name. So if that code is not located in any function or eval context, it'll create the property my_var
on the window
object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With