CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   global variable problem (http://www.codingforums.com/showthread.php?t=275236)

BaronZ 10-05-2012 07:32 AM

global variable problem
 
Code:

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 07: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.

Code:

<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 07:51 AM

Quote:

Originally Posted by Philip M (Post 1276407)
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.

Code:

<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 07:57 AM

Quote:

Originally Posted by BaronZ (Post 1276410)
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/5...variable-scope

Code:

<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 08: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 08:12 AM

Quote:

Originally Posted by BaronZ (Post 1276413)
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 08:17 AM

Quote:

Originally Posted by Philip M (Post 1276414)
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 08:17 AM

kinda different from java

Philip M 10-05-2012 08:28 AM

Quote:

Originally Posted by BaronZ (Post 1276417)
kinda different from java

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


All times are GMT +1. The time now is 08:43 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.