Local And Global Variables In JavaScript

18th March 2012

I have seen a lot of this sort of thing, so though I would put together a quick lesson in JavaScript variable scope. There are some important differences between local and global variables in JavaScript that will cause grey hairs if you don't know what's going on.

Scope is something that dictates what variables can be seen by code. If a variable is created inside a function then it can be said to be local as only the locally running code can see it. A variable that can be accessed by any part of the code is said to be global. This is special in JavaScript, which I will come onto later.

This scope contept is important as if a variable is outside the current scope then the code can't see it. Everything in JavaScript is a variable so understanding how and when variables can be accessed is important.

To create a local variable in JavaScript do this:

  1. var variable;

One thing to note here is that if you create the variable in the global space (i.e. outside of a function scope) then it will be global. If this code is run inside a function then the variable created will be local to that function.

To create a global variable in JavaScript do this:

  1. <script type="text/javascript">
  2. <!--//--><![CDATA[// ><!--
  3.  
  4. variable;
  5.  
  6. //--><!]]>
  7. </script>

This variable will always be global, no matter where it is created.

The massively important thing here is that when JavaScript encounters a global variable declaration it will traverse up the Browser Object Model (BOM) tree to see if any variables with the same name have been set. If they have then it overrides that variable with the one defined here. This happens even if the variable is defined in this way within the scope of a function.

Give it a go by running the following:

  1. // Create a global variable.
  2. var variable = 1;
  3.  
  4. var runMe1 = function() {
  5. console.log('runMe1');
  6. console.log(variable); // prints 1
  7. // Alter the global variable.
  8. variable = 2;
  9. console.log(variable); // prints 2
  10. }
  11.  
  12. var runMe2 = function() {
  13. console.log('runMe2');
  14. console.log(variable); // prints undefined
  15. // Create and set a local variable.
  16. var variable = 1;
  17. console.log(variable); // prints 1
  18. }
  19.  
  20. runMe1();
  21. runMe2();
  22. console.log('root');
  23. console.log(variable); // prints 2

One thing that you might find strange when running this code was the first attempt to print out the 'variable' variable in runMe2() produces an 'undefined' response. However, this does make sense. This happens because JavaScript knows that the variable will be created later on in this function and so will not bring through the global scoped variable of the same name.

So why is this important? Well suppose you included a framework or third part script that had a variable called 'a'. This might seem far fetched, but this sort of thing is quite common in the world of JavaScript minification and closure compilers. Everything will work fine until you try to define a global variable within your own code called 'a' and assign it a value that changes how the included code works. This sort of problem is really, really difficult to track down, especially in minified code. If you create a local variable called 'a' then this will have local scope (i.e. within the current function call) and will not clobber the global variable.

So, bottom line? It's good practice to define your variables local (as in 'var variable;') so that they don't clobber anything else within the scope. Doing this as a matter of course prevents any future code breaks from global variables.

Comments

Permalink
very nice , thankx

Submitted by naresh on Wed, 03/27/2013 - 15:02

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.