Variable Scope

I

ieming.chen

I have a very puzzling question about how JavaScript variable scope
works.

var s = 1;

alert (s); // show s = 1

function show ()
{
alert(s); // show s = undefined, expecting s= 1

var s = 2;
alert(s); // show s = 2
};

Can anybody explain why it is happening this way?
 
J

Joost Diepenmaat

I have a very puzzling question about how JavaScript variable scope
works.

var s = 1;

alert (s); // show s = 1

function show ()
{
alert(s); // show s = undefined, expecting s= 1

var s = 2;
alert(s); // show s = 2
};

Can anybody explain why it is happening this way?

Variable scope in java is function-based. You're defining s in show(),
so all references to "s" in show() refer to that same variable. The fact
that the actual declaration is after the alert() in the source code
doesn't matter. When you enter a function all vars declared in that
function are already instantiated. It's just undefined because the
assignment happens later (at the point of the declaration).

At least that's how I understand it. See Ecma-262, section 10.1.3 for
the official wording.
 
R

RobG

I have a very puzzling question about how JavaScript variable scope
works.

var s = 1;

alert (s); // show s = 1

Alerts value of global s declared above.
function show ()
{
alert(s); // show s = undefined, expecting s= 1

Declarations are processed before any code is executed. Here s refers
to the local s declared below, but it isn't given a value until the code
ie executed so its value is currently undefined.

Your function is effectively:

function show(){
var s;
alert(s);
s = 2;
alert(s);
}

var s = 2;

Now the local s is assigned a value of 2.

alert(s); // show s = 2

And you see it.

};

Can anybody explain why it is happening this way?

Declarations are processed before any code is executed, which is why you
can't conditionally declare functions or variables. It is also why you
can call functions above the code that declares them, e.g.

foo();

function foo(){alert('foo');}
 
T

timothytoe

Variable scope in java is function-based. You're defining s in show(),
so all references to "s" in show() refer to that same variable. The fact
that the actual declaration is after the alert() in the source code
doesn't matter. When you enter a function all vars declared in that
function are already instantiated. It's just undefined because the
assignment happens later (at the point of the declaration).

At least that's how I understand it. See Ecma-262, section 10.1.3 for
the official wording.

You meant JavaScript, not java.

ieming, you might want to do what I do--declare all variables at the
top of their functions so that you and JavaScript have the same
understanding of the situation.

Note that block scope does not apply. Don't assume you can throw some
braces in somewhere and start a new scope.

var s = 1;
alert (s); // show s = 1
function show ()
{
var s = 2;
alert(s); // show s = 2
};
 
J

Joost Diepenmaat

timothytoe said:
You meant JavaScript, not java.

Oops. Yes, obviously. :)
ieming, you might want to do what I do--declare all variables at the
top of their functions so that you and JavaScript have the same
understanding of the situation.

That's probably good advice.
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top