please help me in understanding the output of the below code

Discussion in 'Javascript' started by Ramesh, Apr 1, 2009.

  1. Ramesh

    Ramesh Guest

    var a = 123;
    function f() {
    alert(a);
    var a = 1;
    alert(a);
    }
    f();

    This outputs - first "undefined" and then "1". Why is it so?
    Why it is outputting "undefined" in the first alert even though global
    variable "a" is defined.
     
    Ramesh, Apr 1, 2009
    #1
    1. Advertising

  2. Am Wed, 01 Apr 2009 10:48:32 -0700 schrieb Ramesh:

    > var a = 123;
    > function f() {
    > alert(a);
    > var a = 1;
    > alert(a);
    > }
    > f();
    >
    > This outputs - first "undefined" and then "1". Why is it so? Why it is
    > outputting "undefined" in the first alert even though global variable
    > "a" is defined.


    JS first checks if a variable is declared as local via the var keyword
    somewhere in the current scope before executing the function. Upon the
    first alert() <a> is declared as local (the global <a> is "shadowed"),
    but has yet no value assigned. When reaching the second alert <a> got a
    value assigned.

    Gregor



    --
    http://www.gregorkofler.com
    http://web.gregorkofler.com - vxJS, a JS lib in progress
     
    Gregor Kofler, Apr 1, 2009
    #2
    1. Advertising

  3. Ramesh

    Ramesh Guest

    Thanks very much Gregor. Now I understood why this happens.
    I am new to javascript and read that it is an interpreted language -
    which according to me is interpreted line by line. So I thought why it
    is not considering the already defined global variable.
     
    Ramesh, Apr 1, 2009
    #3
  4. On Wed, 01 Apr 2009 10:48:32 -0700, Ramesh wrote:

    > var a = 123;
    > function f() {
    > alert(a);
    > var a = 1;
    > alert(a);
    > }
    > f();
    >
    > This outputs - first "undefined" and then "1". Why is it so? Why it is
    > outputting "undefined" in the first alert even though global variable
    > "a" is defined.


    Think of your code as being written this way, because this is what is
    happening inside a [hypothetical] Javascript engine.
    (Note: While the actual internals of HOW the engine works may vary, it
    will produce the same results that this code does... variables created
    as the first thing the function does, any assignment deferred to the time
    where the assignment happens.)

    var a = 123;

    function f() {
    // ALL variables that get declared in the function are created first,
    // regardless of where the 'var' keyword appears. This is why one can
    // re-declare the variable without error (though JSLint will catch it).
    var a;

    alert(a); // a is undefined.
    a = 1; // The assignment happens here. Remember, the variable
    // was created coming into the function.
    alert(a); // And now a has a value.
    }
    f();
     
    Jeremy J Starcher, Apr 1, 2009
    #4
  5. Ramesh

    Ramesh Guest

    Thanks very much for the explanation, Jeremy.
     
    Ramesh, Apr 1, 2009
    #5
  6. Ramesh wrote:
    > Thanks very much Gregor. Now I understood why this happens.
    > I am new to javascript and read that it is an interpreted language -


    You have been mislead by a wannabe. AFAWK ECMAScript implementations like
    JavaScript (there really is no single "javascript"[1]) are JIT-compiled to
    byte-code which is *then* interpreted by a Virtual Machine (similar to Java).

    You should throw the book which contains that nonsense away, and remove all
    browser bookmarks which might refer to it.

    > which according to me is interpreted line by line.


    Another misconception. Virtually *no* "interpreted language" is not
    compiled first.


    PointedEars
    ___________
    [1] <http://PointedEars.de/es-matrix/>
     
    Thomas 'PointedEars' Lahn, Apr 2, 2009
    #6
  7. Jeremy J Starcher wrote:
    > // ALL variables that get declared in the function are created first,
    > // regardless of where the 'var' keyword appears. This is why one can
    > // re-declare the variable without error (though JSLint will catch it).


    As will SpiderMonkey (JavaScript 1.5+) in Strict Mode; the result is a warning.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Apr 2, 2009
    #7
  8. Ramesh

    slebetman Guest

    On Apr 2, 5:42 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > Ramesh wrote:
    > > Thanks very much Gregor. Now I understood why this happens.
    > > I am new to javascript and read that it is an interpreted language -

    >
    > > which according to me is interpreted line by line.

    >
    > Another misconception.  Virtually *no* "interpreted language" is not
    > compiled first.
    >


    True, however some languages (tcl, Perl), even though compiled,
    maintains the line-by-line semantics when determining stuff such as
    "when does variable $x exist?". So it's not a completely unreasonable
    assumption to be made by someone used to scripting languages.
     
    slebetman, Apr 2, 2009
    #8
    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. KK
    Replies:
    2
    Views:
    581
    Big Brian
    Oct 14, 2003
  2. joshc
    Replies:
    14
    Views:
    786
    Keith Thompson
    Jan 14, 2005
  3. DeltaOne
    Replies:
    31
    Views:
    875
    S.Tobias
    May 20, 2005
  4. kiran
    Replies:
    12
    Views:
    1,125
    Scott Sauyet
    Dec 7, 2011
  5. jagan
    Replies:
    4
    Views:
    527
Loading...

Share This Page