2

This might be a duplicate but I did not know how to search for it.

Why does

var test = test || {};

work, but

var test = testing || {};

throws an error? At the point of definition both test and testing are undefined.

Edit

The error thrown is "Reference Error: testing is not defined"

3
  • @MattLin: But I did not define test either, or did I?
    – Amberlamps
    Nov 22, 2012 at 10:53
  • @nozzleman: What? Just copy and paste var test = testing || {}; to your console.
    – Amberlamps
    Nov 22, 2012 at 10:57
  • @Amberlamps yeah, nevermind, i got your question wrong, thought you did declare var testing before...
    – nozzleman
    Nov 22, 2012 at 11:17

3 Answers 3

3

There's a difference between a variable being undefined in the sense of not existing and a variable holding the value undefined.

In your first example you declare test with var such that when the expression on the right of the = is evaluated the variable test exists but has the value undefined.

In your second example you haven't defined testing at all, hence the error.

EDIT: It occurs to me that perhaps further explanation wouldn't hurt.

To simplify, the JavaScript engine takes two passes through the code. The first pass is the parse/compile phase, and it is then that variable declarations but not assignments happen. The second pass is the actual execution, and it is then that assignments occur. This results in an effect often called "variable hoisting" - it is as if the JS engine "hoists" the declarations to the top of their scope but still does the assignments in place.

So as to the point of code like this:

var test = test || {};

...it is basically saying "Does test already exist with a truthy value? If so use it, otherwise set it to a new empty object."

The JS engine doesn't mind if the same variable is declared more than once in the same scope - it basically ignores the second declaration, but doesn't ignore any assignment included with the second declaration. So if test is declared in some other script block, perhaps in a separate JS include file, then the line in question just assigns test to itself (assuming it has a truthy value, where all objects are truthy). But if it hasn't been declared elsewhere it will still exist as a result of the current var statement but it will be undefined before the current assignment so then the || operator returns the right-hand operand, the {}.

1
  • 1
    There's a difference between a variable being undefined in the sense of not existing and a variable holding the value undefined.. This is the line. +1
    – Jashwant
    Nov 22, 2012 at 10:57
1

It's due to the var keyword. Because the variable is declared it also exists albeit with the value undefined. What the || then does is to check for truthiness and when it finds that the object is undefined it gives you a new object to work with.

The latter does the exact same but since you're doing it on "one line" the testing object isn't defined when evaluated and thusly throws the exception.

1
var test = test || {};

Here, test has been declared but not defined

but in,

var test = testing || {};

There's no reference of testing whatsoever and you are still trying to assign its value.

A corresponding code for the first case would be,

var testing;  // See testing is still undefined
var test = testing || {};

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.