Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8874751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:48:05+00:00 2026-06-14T18:48:05+00:00

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

  • 0

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”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:48:07+00:00Added an answer on June 14, 2026 at 6:48 pm

    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 {}.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this might seem like a duplicate, but I'm not getting anywhere with
Ok.. This might be the duplicate.. but I dont think I even know the
No doubt this might be a duplicate question but I am not able to
Note: This might look like duplicate but its not. I have created a .net(WCF)
Possible Duplicate: How is std::iostream buffered? This might sound ridiculous, but how can I
Now this might look like a duplicate thread, but my question is that I
Possible Duplicate: Calculating the Difference Between Two Java Date Instances I know this might
This might be a duplicate question but as you can see they didn't get
I know you guys going to say this might be possible duplicate to PHP
This might sound like a duplicate, but I have searched through the forum questions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.