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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:36:57+00:00 2026-06-11T21:36:57+00:00

Possible Duplicate: Confused by Javascript's variable scope For example, this is my JavaScript code:

  • 0

Possible Duplicate:
Confused by Javascript's variable scope

For example, this is my JavaScript code:

var foo = 'Originalvalue';
var foo = 'Nextvalue';
alert(foo); // Nextvalue

So then, now I am sure writing var in front of an already-declared variable just simply is nullified and of no use to the program.

But then consider this program:

var foo = 'Originalvalue';
function duhfoo() {
  var foo = 'Newbievalue';
}
duhfoo();
alert(foo); // Originalvalue

Then, from the logic explained in my first example, the value should be ‘Originalvalue’, as there is already a variable called foo. Then why is it like so?

  • 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-11T21:36:58+00:00Added an answer on June 11, 2026 at 9:36 pm

    In Javascript there are two kinds of variables: local variables and global variables.

    When using var outside of functions you are declaring a global variable and the same happens if you don’t use var at all. Writing

    foo = "first";
    

    at top level (outside any function) is the same as var foo = "first".

    When inside a function however things are different, and the keyword var discriminates between local and global variables:

    var foo = "first";
    var bar = "second";
    
    function f()
    {
        var foo = "third"; // local
        bar = "fourth";    // global
    }
    
    f();
    alert([foo, bar]); // output will be first,fourth
    

    In other words when you use var inside a function the variable will be a different one with the same name, visible only by code written inside the boundaries of the function.

    Please note that the boundary is determined by the function, and not the braces {...}. If you have nested blocks and use another var declaration inside the blocks the variable will be the same and this is different from what happens in other languages like Java, C or C++.

    The only way to create a scope is to define a function (including a function inside a function).

    Another very important thing to remember in Javascript (especially if having been exposed to similar-looking languages in which this concept is not present like Java, C or C++) is the idea of “capture”/”closure”…

    var foo = "first";
    
    function f()
    {
        // Local variable
        var foo = "second";
    
        function g()
        {
            // This is the local foo of f, not the global
            // one even if there is no "var" declaration
            // inside this nested scope
            return foo;
        }
    
        return g;
    }
    
    var nested_function = f();
    
    alert([foo, nested_function()]); // output will be first,second
    

    Basically a local variable can “outlive” the function that defined it, by being used by other functions that are said to “capture” that variable. A function that captures one or more variable is called “closure”.

    In other words a local variable is only visible inside the body of the function, but it may live longer than then function like it happens for the local foo of last example in which the variable survived after returning from f because it has been captured by closure g.

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

Sidebar

Related Questions

Possible Duplicate: What underlies this JavaScript idiom: var self = this? I am confused
Possible Duplicate: Use of 'prototype' vs. 'this' in Javascript? I am confused with these
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: Difference between using var and not using var in JavaScript Hello, I
Possible Duplicate: What is the !! operator in JavaScript? Sorry if this one is
Possible Duplicate: Property Declaration and Automatic Backing Storage Allocation I'm really confused about this
Possible Duplicate: javascript variable into php Okay lets imagine I've a javascript variable x
Possible Duplicate: Null object in javascript Hi, I've read this thread about null in
Possible Duplicate: Confused about C macro expansion and integer arithmetic A riddle (in C)
Possible Duplicate: Concatenate Two NSDate String values Just confused that is there any default

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.