Hi there, today I’m going to show how you can create global and local variables in JavaScript and also try to explain why you should try to avoid creating global variables in your code, of course I’m not saying that you can’t use it, I’m only saying that if you can find another solution that will use local variables, then you should go for it, but sometimes we know that using global it’s the only way, so try to use it wisely. That’s the best advice I can give you.
I know that a lot of developers think it’s really convenient to create global variables because it’s “THERE” you can always access it. But the problem is, they are created in the moment your code is loaded and it doesn’t matter whether you use it or not it will stays in the memory until your page is closed, in other words you’re wasting resources that could have been used for other things, on the other hand local variables are created when the scope (method) they belong is invoked and assigned to be destroyed by the garbage collector right after its execution. As your application grows and become more complex you have to consider all these small details.
Creating global variables
// Right way to declare a global variable.
var AppName = “MyCoolApp”;
// It works but it’s not a good practice to declare like this.
Version = 1;
Accessing global variables
function CallingGlobal()
{
// Global.
alert(AppName + ” “ + Version); // Result -> MyCoolApp 1
Version = 2;
alert(AppName + ” “ + Version); // Result -> MyCoolApp 2
}
Accessing global and local variables
function CallingLocal()
{
// Local.
var Version = 3;
alert(Version); // Result -> 3
// Global.
alert(AppName + ” “ + window.Version); // Result -> MyCoolApp 2
}
I suggest to do like this:
function CallingMethods()
{
alert(GetAppName() + ” “ + GetVersion());
}
function GetAppName()
{
return “MyCoolApp”;
}
function GetVersion()
{
return 1;
}
I don’t know if it’s the BEST way to do it, but at least you can see it’s more organized having functions to retrieve values, so if you think I’m wrong, send me a comment.
To download the code, click here -> Global and Local Variables in JavaScript
Author: Luciano Sampaio
February 12th, 2008 at 9:44 am
Hi Luciano,
Thanks again for the advice… I am finding that your web site is easier to understand than w3schools and asp101… keep it up, as I believe in time, your posts are going to be looked for all over the world.
Dave
February 13th, 2008 at 1:21 pm
Thanks again David!!! The first tutorials were just to get used to the new experience(at least for me) of blogging. The next tutorials will be even better.
March 12th, 2008 at 9:00 pm
Thank you. I am just learning js and this page helped a ton. I liked the way that you gave advice against globals. It helped me realize that I was trying to use them when it wasnt good technique.
March 13th, 2008 at 9:54 am
Hi Neil, Thank you for the comment. I’m glad I could help, You said you’re learning JavaScript, so you’ll love the next tutorial: “The TOP 10 SUPER JavaScript Methods”.