please help me in understanding the output of the below code

R

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

Gregor Kofler

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
 
R

Ramesh

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

Jeremy J Starcher

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();
 
T

Thomas 'PointedEars' Lahn

Ramesh said:
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/>
 
T

Thomas 'PointedEars' Lahn

Jeremy said:
// 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
 
S

slebetman

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top