0

If I remove "var", the browser(Chrome) complains : "someVar is not defined"

var someVar = someVar || {};

From what I know, it should complain only if I am not initializing the variable.

Paddy
  • 609
  • 7
  • 25
  • 2
    Without the `var`... you aren't initializing it. So it complains. – Niet the Dark Absol Dec 10 '14 at 15:49
  • But if I say "someVar = 5;", I do not need a "var". Is it something to do with the conditional nature of initialization? – Paddy Dec 10 '14 at 15:51
  • It's a warning, not an error. Correct? – Jonathan Wheeler Dec 10 '14 at 15:51
  • it is "Uncaught ReferenceError" – Paddy Dec 10 '14 at 15:53
  • possible duplicate of [What is the function of the var keyword and when to use it (or omit it)?](http://stackoverflow.com/q/1470488/1048572) – Bergi Dec 10 '14 at 15:54
  • These are all entirely wrong. Usually, constructs like this are used to prevent a variable from _not being_ defined _but_ take its "outer" value if it is already defined there. Whereas "outer" means somewhere else in the application where the script is used. – Axel Amthor Dec 10 '14 at 15:54
  • @NiettheDarkAbsol Without 'var' shouldn't it just initialize it as a global var? So what is the difference between this: a = {} (this works without problems) and a = a || {} (this throws an error) and var a = a || {} (this works, too). I am actually curious about this. – Fygo Dec 10 '14 at 15:55
  • 1
    `a = 5` does *not* try to **read** `a`. `a = a || 5` tries to read `a`, so if it doesn't exist, an error is thrown. – Felix Kling Dec 10 '14 at 15:56
  • @Bergi, I could not find a discussion on conditional value in the referenced thread. – Paddy Dec 10 '14 at 15:56
  • *Setting* an uninitialised var will default to a global variable. *Getting* an uninitialised var will error. – Niet the Dark Absol Dec 10 '14 at 15:57
  • 1
    @Paddy: It explains the difference between declared and undeclared variables. Accessing an undeclared variable will throw a reference error. – Bergi Dec 10 '14 at 15:58
  • @FelixKling must be right. "someVar = 5 || {};" works fine. – Paddy Dec 10 '14 at 15:58
  • @Bergi Yes, I had missed that (pretty simple) deduction :-( – Paddy Dec 10 '14 at 15:59

1 Answers1

4

From what I know, it should complain only if I am not initializing the variable.

But you are trying to read an undeclared variable. Given

someVar = someVar || {};

it first evaluates someVar || {} and tries to get the value of someVar in the process. Since someVar doesn't exist (yet), you get a reference error.

Here is an example that does not throw an error:

someVar = true || someVar;

Because || does shirt-circuiting evaluation, the second operand is never evaluated and thus no error is thrown.

Why does it work with var then?

You might think that using the var keyword wouldn't make a difference since someVar || {} is still evaluated first. However, due to hoisting, the statement

var someVar = someVar || {};

is actually evaluated as

var someVar;
someVar = someVar || {};

So at the moment someVar is read, it is already declared.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • So, I got confused with the hoisting thing, and this link discuses the concept in more mundane manner : http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html – Paddy Dec 10 '14 at 18:35