...

View Full Version : global variable problem



BaronZ
10-05-2012, 08:32 AM
if(true){
var a = 2;//i thought 'a' is a local variable here.
}
alert(a);//but here it pops up '2',which means 'a' is a global variable?why?

Philip M
10-05-2012, 08:43 AM
Why do you think that a is global? If your code is within a function then a is local, and alert (a) results in the value of a being alerted.


<script type="text/javascript">

test();

var a = 10;
function test() {
var a = 12; // note the var keyword defines a new variable local to the function. But it is obviously silly to duplicate names in this way.
alert ("Local " + a); //12
}
alert ("Global " + a); //10

</script>

if(true) is meaningless as the condition will always be true.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

BaronZ
10-05-2012, 08:51 AM
Why do you think that a is global? If your code is within a function then a is local, and alert (a) results in the value of a being alerted.


<script type="text/javascript">

var a =10;
function test() {
var a = 12;
alert ("Local " + a); //12
}
alert ("Global " + a); //10

test();

</script>

if(true) is meaningless as the condition will always be true.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
umm,but if a is not global,why the alert box shows '2'?

Philip M
10-05-2012, 08:57 AM
umm,but if a is not global,why the alert box shows '2'?

I think you need to better understand global and local scope. You seem to be unclear. Study the example I have given you.

http://stackoverflow.com/questions/500431/javascript-variable-scope


<script type="text/javascript">

test();

var a = 10;
function test(){
if(true) {
var a = 40;
}
alert (a); // within the function alerts '40', not the global value of '10'
}
alert (a); // outside the function alerts global value of '10'

</script>

BaronZ
10-05-2012, 09:08 AM
i understand the example u showed me.but in the code i showed above,i think 'a' is a local variable,so in my opinion,alert(a) should pop up 'undefined' or sth,but it's '2',that's what confused me.

Philip M
10-05-2012, 09:12 AM
i understand the example u showed me.but in the code i showed above,i think 'a' is a local variable,so in my opinion,alert(a) should pop up 'undefined' or sth,but it's '2',that's what confused me.

If your code fragment is not contained within a function then the variable a is global scope. Do what I say - study the topic.

BaronZ
10-05-2012, 09:17 AM
If your code fragment is not contained within a function then the variable a is global scope. Do what I say - study the topic.
ic now:)

BaronZ
10-05-2012, 09:17 AM
kinda different from java

Philip M
10-05-2012, 09:28 AM
kinda different from java

Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum