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() {
    var a = 1;

    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. Advertisements

  2. Am Wed, 01 Apr 2009 10:48:32 -0700 schrieb Ramesh:
    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 Kofler, Apr 1, 2009
    1. Advertisements

  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
  4. 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.
    Jeremy J Starcher, Apr 1, 2009
  5. Ramesh

    Ramesh Guest

    Thanks very much for the explanation, Jeremy.
    Ramesh, Apr 1, 2009
  6. 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.
    Another misconception. Virtually *no* "interpreted language" is not
    compiled first.

    [1] <>
    Thomas 'PointedEars' Lahn, Apr 2, 2009
  7. As will SpiderMonkey (JavaScript 1.5+) in Strict Mode; the result is a warning.

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

    slebetman Guest

    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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.