Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-05-2012, 07:32 AM   PM User | #1
BaronZ
New to the CF scene

 
Join Date: Oct 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
BaronZ is an unknown quantity at this point
Question 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?
BaronZ is offline   Reply With Quote
Old 10-05-2012, 07:43 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-05-2012 at 07:51 AM..
Philip M is online now   Reply With Quote
Old 10-05-2012, 07:51 AM   PM User | #3
BaronZ
New to the CF scene

 
Join Date: Oct 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
BaronZ is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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'?
BaronZ is offline   Reply With Quote
Old 10-05-2012, 07:57 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by BaronZ View Post
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>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-05-2012 at 08:08 AM..
Philip M is online now   Reply With Quote
Users who have thanked Philip M for this post:
BaronZ (10-05-2012)
Old 10-05-2012, 08:08 AM   PM User | #5
BaronZ
New to the CF scene

 
Join Date: Oct 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
BaronZ is an unknown quantity at this point
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.
BaronZ is offline   Reply With Quote
Old 10-05-2012, 08:12 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by BaronZ View Post
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is online now   Reply With Quote
Users who have thanked Philip M for this post:
BaronZ (10-05-2012)
Old 10-05-2012, 08:17 AM   PM User | #7
BaronZ
New to the CF scene

 
Join Date: Oct 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
BaronZ is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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 is offline   Reply With Quote
Old 10-05-2012, 08:17 AM   PM User | #8
BaronZ
New to the CF scene

 
Join Date: Oct 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
BaronZ is an unknown quantity at this point
kinda different from java
BaronZ is offline   Reply With Quote
Old 10-05-2012, 08:28 AM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by BaronZ View Post
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 the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is online now   Reply With Quote
Reply

Bookmarks

Tags
javaxcript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:32 AM.


Advertisement
Log in to turn off these ads.