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 7278521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:53:09+00:00 2026-05-28T22:53:09+00:00

Today, I got completely surprised when I saw that a global variable has undefined

  • 0

Today, I got completely surprised when I saw that a global variable has undefined value in a certain case.

Example:

var value = 10;
function test() {
    //A
    console.log(value);
    var value = 20;

    //B
    console.log(value);
}
test();

Gives output as

undefined
20

Here, why is the JavaScript engine considering global value as undefined? I know that JavaScript is an interpreted language. How is it able to consider variables in the function?

Is that a pitfall from the JavaScript engine?

  • 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-05-28T22:53:09+00:00Added an answer on May 28, 2026 at 10:53 pm

    This phenomenon is known as: JavaScript Variable Hoisting.

    At no point are you accessing the global variable in your function; you’re only ever accessing the local value variable.

    Your code is equivalent to the following:

    var value = 10;
    
    function test() {
        var value;
        console.log(value);
    
        value = 20;
        console.log(value);
    }
    
    test();
    

    Still surprised you’re getting undefined?


    Explanation:

    This is something that every JavaScript programmer bumps into sooner or later. Simply put, whatever variables you declare are always hoisted to the top of your local closure. So, even though you declared your variable after the first console.log call, it’s still considered as if you had declared it before that.
    However, only the declaration part is being hoisted; the assignment, on the other hand, is not.

    So, when you first called console.log(value), you were referencing your locally declared variable, which has got nothing assigned to it yet; hence undefined.

    Here’s another example:

    var test = 'start';
    
    function end() {
        test = 'end';
        var test = 'local';
    }
    
    end();
    alert(test);
    

    What do you think this will alert? No, don’t just read on, think about it. What’s the value of test?

    If you said anything other than start, you were wrong. The above code is equivalent to this:

    var test = 'start';
    
    function end() {
        var test;
        test = 'end';
        test = 'local';
    }
    
    end();
    alert(test);
    

    so that the global variable is never affected.

    As you can see, no matter where you put your variable declaration, it is always hoisted to the top of your local closure.


    Side note:

    This also applies to functions.

    Consider this piece of code:

    test("Won't work!");
    
    test = function(text) { alert(text); }
    

    which will give you a reference error:

    Uncaught ReferenceError: test is not defined

    This throws off a lot of developers, since this piece of code works fine:

    test("Works!");
    
    function test(text) { alert(text); }
    

    The reason for this, as stated, is because the assignment part is not hoisted. So in the first example, when test("Won't work!") was run, the test variable has already been declared, but has yet to have the function assigned to it.

    In the second example, we’re not using variable assignment. Rather, we’re using proper function declaration syntax, which does get the function completely hoisted.


    Ben Cherry has written an excellent article on this, appropriately titled JavaScript Scoping and Hoisting.
    Read it. It’ll give you the whole picture in full detail.

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

Sidebar

Related Questions

Today I got here nice answer that will resolve my problem. Unfortunately, I forgot
I asked a question here earlier today and got that fixed, but now I
I have an app on appstore that uses notifications. Today I got a support
Today I got my book Head First Design Patterns in the mail. Pretty interesting
Today I got this question for which I think I answered very bad. I
today I got confused when doing a couple of <%=Html.LabelFor(m=>m.MyProperty)%> in ASP.NET MVC 2
I just started reading Effective C++ today and got to the point where the
I got this error today when trying to open a Visual Studio 2008 project
I got an error today while trying to do some formatting to existing code.
I got to thinking today: what is the best way of getting a distinct

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.