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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:12:56+00:00 2026-05-17T01:12:56+00:00

var foo = ‘hello’; var myfunc = function() { console.log(foo); var foo = foo

  • 0
var foo = 'hello';

var myfunc = function() {
  console.log(foo);
  var foo = foo || 'world';
  console.log(foo);
}

myfunc();

why is the first foo logged to be ‘undefined’ ?

  • 1 1 Answer
  • 1 View
  • 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-17T01:12:56+00:00Added an answer on May 17, 2026 at 1:12 am

    Because on which line you actually declare a variable using “var” is irrelevant, as long as it remains in the same function. If a function has a var x declared anywhere within it, then any reference to that name is considered local to the scope where it is declared.

    Of course, normally you don’t reference a variable before it’s declared, but consider this snippet:

    function foo(a) {
       if (a) {
         var b = "something";
       }
       console.log(b);
    }
    

    Variable b is local to that function, hence whatever the value of a, usage of b won’t accidentally refer to a variable declared on an enclosing scope.

    Note: javascript only has function level scoping, it has no block level scoping.

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

Sidebar

Related Questions

Given: function foo(){ var bar = quux; console.log(/*mystery code here*/); } I'm looking for
var foo = { p1: function(){ return this.p2; }, p2: function(){ console.log('i am foo.p2');
I see this throughout our codebase: (function(undefined) { var foo = { dirtyRow: undefined,
var foo = function () { this.bar = 1; } >> foo.bar undefined How
If you execute this code: var foo = {bar: 'baz'}; window.console.log(foo); foo.bar = 'bla';
var foo = 'bar'; console.log(window.foo); // bar Seems like variables get assigned as properties
When I do var foo = $('n'); console.log(foo); I get jQuery object. However if
var Foo = Foo || {}; Foo.Controller = (function ($) { var $page =
var foo = { x: 1, y: (function () { return ++this.x; })() };
How can we differentiate between var foo; and var foo=undefined; ? typeof foo will

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.