Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare variable without var keyword and logical OR

Tags:

javascript

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?

like image 357
John Giotta Avatar asked Aug 10 '11 15:08

John Giotta


People also ask

How does variable behave without VAR?

If you declare a variable, without using "var", the variable always becomes GLOBAL.

Is it necessary to use keyword while declaring variables?

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.

Is it necessary to use var keyword while declaring variable in JavaScript?

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 .

Can you declare a variable without value in JavaScript?

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 .


2 Answers

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;
like image 99
Daniel Baulig Avatar answered Nov 07 '22 04:11

Daniel Baulig


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.

like image 36
jAndy Avatar answered Nov 07 '22 04:11

jAndy