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

  • Home
  • SEARCH
  • 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 8389649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:47:08+00:00 2026-06-09T18:47:08+00:00

First , let’s see the code. var a=0; b=1; document.write(a); function run(){ document.write(b); var

  • 0

First , let’s see the code.

var a=0;
b=1;
document.write(a);
function run(){
    document.write(b);
    var b=1;
}
run();

I think the result is 01 .but in fact , The result is 0undefined.

Then I modify this code.

var a=0;
b=1;
document.write(a);
function run(){
    document.write(this.b); //or document.write(window.b)
    var b=1;
}
run();

Yeah, this time it runs as expected. 01 . I can’t understand, WHY?

More interesting, I modify the code again .

var a=0;
b=1;
document.write(a);
function run(){
    document.write(b);
    //var b=1;       //I comment this line
}
run();

The result is 01.

So , Can anyone explain this?

Thanks for share your viewpoints.
I simplify this code

b=1;
function run(){
    console.log(b); //1
}

two:

b=1;
function run(){
    var b=2;
    console.log(b); //2
}

three:

b=1;
function run(){
    console.log(b); //undefined
    var b=2;
}
  • 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-09T18:47:10+00:00Added an answer on June 9, 2026 at 6:47 pm

    When you refer to a variable within a function JS first checks if that variable is declared in the current scope, i.e., within that function. If not found it looks in the containing scope. If still not found it looks in the next scope up, and so forth until finally it reaches the global scope. (Bear in mind that you can nest functions inside each other, so that’s how you get several levels of containing scope though of course your exmaple doesn’t do that.)

    The statement:

    b=1;
    

    without var declares a global variable that is accessible within any function, except that then in your first function you also declare a local b. This is called variable shadowing.

    “But”, you say, “I declare the local b after document.write(b)“. Here you are running into declaration “hoisting”. A variable declared anywhere in a function is treated by the JS interpreter as if it had been declared at the top of the function (i.e., it is “hoisted” to the top), but, any value assignment happens in place. So your first function is actually executed as if it was like this:

    function run(){
        var b;              // defaults to undefined
        document.write(b);  // write value of local b
        b=1;                // set value of local b
    }
    

    In your second function when you use this.b, you’ll find that this refers to window, and global variables are essentially properties of window. So you are accessing the global b and ignoring the local one.

    In your third function you don’t declare a local b at all so it references the global one.

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

Sidebar

Related Questions

First let me say that I did see this article: How to remove AspxAutoDetectCookieSupport
First let me say, I am not a coder but I help manage a
first let me say I know the following code will be considered bad practices..
First let me say I've never used PHP but I'm looking to learn it,
First let me show some code. class User has_and_belongs_to_many :roles named_scope :employees, { :conditions
First let me note, that I use AspectJ and I like it, but what
First let me say sorry for the amount of code I'm posting below I'm
I have a problem with js menu code. first let me explain what I
First let me apologize for the scale of this problem but I'm really trying
First let me start with an example, Public Class EmpDemo Public Function getEmpData() as

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.