| View previous topic :: View next topic |
| Author |
Message |
Jabarikh New member

Joined: 19 Sep 2012 Posts: 1
|
Posted: Wed Sep 19, 2012 8:54 am Post subject: What is the difference between Global and Local Variables? |
|
|
| What is the difference from a functional stand point and is there any overlap in their capabilities? |
|
| Back to top |
|
 |
sohnee Senior Member

Joined: 17 Jul 2002 Posts: 2077 Location: UK
|
Posted: Thu Oct 11, 2012 2:41 am Post subject: |
|
|
Here is an example.
| Code: |
var myVar = 1; // global
function myFunction () {
var myVar = 2; // local
alert(myVar); // 2
}
function myOtherFunction() {
alert(myVar); // 1
}
|
Where possible, you should avoid global variables. _________________ I also work on... Steve Fenton's Blog and contribute to The Enhance PHP Unit Testing Framework |
|
| Back to top |
|
 |
phil karras Senior Member

Joined: 15 Jul 2002 Posts: 1776 Location: MD
|
Posted: Tue Oct 16, 2012 7:31 am Post subject: |
|
|
| Quote: | | What is the difference from a functional stand point and is there |
The difference is that a global var can be used within any function created in the html file or in the js file(s) called into the html file.
A local var can only be used within the function it is created in.
Overlap: Yes & no, yes: a global can be used as if it were a local, but this is not advised, and no: a local can not be used as a global.
sohnee has said it all, | Quote: | | Where possible, you should avoid global variables. |
This is because a global can really mess things up if one is not very careful to only use it as intended. A global should be used ONLY if there is no other way to do the job. _________________ Phil K
Circle Software Consulting
Test website: http://cs.yrex.com/
Guidelines for Posting: http://www.jsworkshop.com/posting.html
IHBAAA = It Has Been Asked And Answered
KISS: http://www.jsworkshop.com/bb/viewtopic.php?t=508 |
|
| Back to top |
|
 |
|