Book Section
-----
TY JavaScript 3rd Ed.
Teach Yourself JS 1.5
Teach Yourself DHTML
Teach Yourself JS 1.3
LLWW: JavaScript

General Section
-----
Discussion Forum
Articles / Tips
JavaScript Links
About the Author
Privacy Policy
Contact Me



Other Sites
-----
Website Workshop
JavaScript Weblog

JavaScript Workshop Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
What is the difference between Global and Local Variables?

 
Post new topic   Reply to topic    JSWorkshop Forum Index -> JavaScript
View previous topic :: View next topic  
Author Message
Jabarikh
New member
New member


Joined: 19 Sep 2012
Posts: 1

PostPosted: Wed Sep 19, 2012 8:54 am    Post subject: What is the difference between Global and Local Variables? Reply with quote

What is the difference from a functional stand point and is there any overlap in their capabilities?
Back to top
View user's profile Send private message
sohnee
Senior Member
Senior Member


Joined: 17 Jul 2002
Posts: 2077
Location: UK

PostPosted: Thu Oct 11, 2012 2:41 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
phil karras
Senior Member
Senior Member


Joined: 15 Jul 2002
Posts: 1776
Location: MD

PostPosted: Tue Oct 16, 2012 7:31 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    JSWorkshop Forum Index -> JavaScript All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
(c) 1997-2002 Starling Technologies and Michael Moncur. Portions (c) Sams Publishing.