What is the scope of Javascript variables?

Discussion in 'Javascript' started by Trev, Oct 29, 2006.

  1. Trev

    Trev Guest

    Hi all,
    Are variables in javascript local to the page, or to the <script> tags
    they are defined within?
    Say I had a bit of code as follows:

    <HEAD>

    <script>

    var myVar = MyFunction();

    </script>

    </HEAD>

    could I then use myVar as follows?

    <BODY>

    <script>

    MyOtherFunction( myVar );

    </script?

    </BODY>
     
    Trev, Oct 29, 2006
    #1
    1. Advertising

  2. Hi,

    Trev wrote:
    > Hi all,
    > Are variables in javascript local to the page, or to the <script> tags
    > they are defined within?
    > Say I had a bit of code as follows:
    >
    > <HEAD>
    >
    > <script>
    >
    > var myVar = MyFunction();
    >
    > </script>
    >
    > </HEAD>
    >
    > could I then use myVar as follows?
    >
    > <BODY>
    >
    > <script>
    >
    > MyOtherFunction( myVar );
    >
    > </script?
    >
    > </BODY>


    Yes, all script content (in internal script tags, or included through
    script src) is interpreted in one "application" only.

    If you define something (variable, function...) in one place of the
    page, you can use it from another place in the same page without doing
    anything. This causes problems especially in pages built with
    server-side controls, because each control must make sure that each
    variable is unique. If two controls define a global variable or a global
    function with the same name, the last one to be parsed wins. That's why
    global variable or functions are a bad idea as soon as the page gets a
    little too complex.

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion, Oct 29, 2006
    #2
    1. Advertising

  3. Trev

    Trev Guest

    Thanks, my friend. I am perplexed though because I have done the
    following:

    <HEAD>

    <script>

    var myBoolean=new Boolean(true);

    // Do other processing, alter value of myBoolean accordingly

    </script>

    </HEAD>


    <BODY>

    <script>

    if (myBoolean) //**************
    {
    // Do other stuff
    }

    </script>

    </BODY>

    My browser (Netscape 6) displays an error, saying that "Error:
    myBoolean is not defined" at the line marked above with the asterisks.
    Whats gone wrong? It all looks fine to me....

    TIA

    Trev
     
    Trev, Oct 30, 2006
    #3
  4. Trev

    Trev Guest

    I don't know if this is relevant, but the if statement in the second
    script tag is encased within a function:


    <BODY>

    <script>

    function DoMyStuff(){

    if (myBoolean) //**************
    {
    // Do other stuff
    }

    }
    </script>

    </BODY>
     
    Trev, Oct 30, 2006
    #4
  5. Trev

    Randy Webb Guest

    Trev said the following on 10/30/2006 12:51 PM:
    > Thanks, my friend. I am perplexed though because I have done the
    > following:


    <snip>

    > <script>
    >
    > var myBoolean=new Boolean(true);


    var myBoolean = true;

    No need for the new Boolean.

    > // Do other processing, alter value of myBoolean accordingly


    <snip>

    > if (myBoolean) //**************
    > {
    > // Do other stuff
    > }


    <snip>

    > My browser (Netscape 6) displays an error, saying that "Error:
    > myBoolean is not defined" at the line marked above with the asterisks.
    > Whats gone wrong? It all looks fine to me....


    NS6? NS is up to version 8 and 6 is considerably old. It may be a bug in
    NS6 that didn't handle new Boolean() <shrug>. Try changing it to just =
    true, test it and see what happens. If it still throws the error, insert
    alerts to debug it to see where it "disappears" at. And then look into
    upgrading your NS :) Firefox is free and NS6/7 are based on Firefox (to
    an extent anyway), and NS8 uses two rendering engines - IE and Mozilla.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Oct 30, 2006
    #5
  6. Trev wrote:

    [snip]

    > var myBoolean=new Boolean(true);


    [snip]

    > if (myBoolean) //**************


    This is unlikely to achieve what you expect. The variable, myBoolean,
    will reference an object. References always evaluate to true when
    type-converted to boolean so even:

    new Boolean(false)

    would pass that test. If you were to insist on using a Boolean object,
    you would have to make use of the valueOf method:

    if (myBoolean.valueOf())

    to obtain the boolean value represented by the object. However, as Randy
    pointed out, a boolean value will do.

    [snip]

    Mike
     
    Michael Winter, Oct 31, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    961
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    672
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    488
  4. Xah Lee
    Replies:
    0
    Views:
    2,249
    Xah Lee
    Feb 26, 2009
  5. Talha Oktay
    Replies:
    8
    Views:
    218
Loading...

Share This Page