# JavaScript Global Variables Explained with Examples

**URL:** <https://forum.freecodecamp.org/t/javascript-global-variables-explained-with-examples/14675>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 26, 2016, 4:44pm UTC](https://forum.freecodecamp.org/t/javascript-global-variables-explained-with-examples/14675 "2016-06-26T16:44:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 26, 2016, 4:44pm UTC](https://forum.freecodecamp.org/t/javascript-global-variables-explained-with-examples/14675/1 "2016-06-26T16:44:51Z")

</div>

Global variables are declared outside of a function for accessibility throughout the program, while local variables are stored within a function using `var` for use only within that function’s [scope](https://developer.mozilla.org/en-US/docs/Glossary/Scope). If you declare a variable without using `var` , even if it’s inside a function, it will still be seen as global:

```auto
var x = 5; //global variable
function someThing(y) {
    var z = x + y;
    console.log(z);
}

function someThing(y) {
    x = 5; //still a global variable!
    var z = x + y;
    console.log(z);
}

function someThing(y) {
    var x = 5; //local variable
    var z = x + y;
    console.log(z);
}

```

A global variable is also an object of the current scope, such as the browser window:

```auto
var dog = “Fluffy”;
console.log(dog); //Fluffy;

var dog = “Fluffy”;
console.log(window.dog); //Fluffy

```

It’s a best practice to minimize global variables. Since the variable can be accessed anywhere in the program, they can cause strange behavior.

References:

- [var -JavaScript|MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var)
- [You Don’t Know JavaScript: Scopes & Closures](https://github.com/getify/You-Dont-Know-JS/tree/master/scope%20%26%20closures)

Additional Info:

- [JavaScript Best Practices: Avoid Globals](http://www.w3.org/wiki/JavaScript_best_practices#Avoid_globals)
- 
## [What’s the difference between a global var and a window.variable in javascript?](https://stackoverflow.com/questions/6349232/whats-the-difference-between-a-global-var-and-a-window-variable-in-javascript)

The scope of JavaScript variables are either global or local. Global variables are declared OUTSIDE the function and its value is accessible/changeable throughout the program.

You should ALWAYS use **var** to declare your variables (to make locally) else it will install GLOBALLY

Take care with the global variables because they are risky. Most of the time you should use closures to declare your variables. Example:

```auto
    (function(){
      var myVar = true;
    })();

```

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [August 7, 2017, 1:47am UTC](https://forum.freecodecamp.org/t/javascript-global-variables-explained-with-examples/14675/2 "2017-08-07T01:47:19Z")

</div>


