Question: I am reading a section about closures in a JS book, and reached an example in the section showing 'getter/Stetter' functions...

```
var getValue, setValue;

(function (){

    var secret = 0;

    getValue = function(){
    return secret;
    }
    setValue = function(v){
    secret = v;
    }

})();

setValue(5);
getValue();
5 //get 5

```

1) I thought omitting 'var' made variables global?

2) From time to time I hear folks talking about 'getters/Setters', What would be a practical use of them?

Thanks!

Answer: 
Why isn't it global?
====================

Javascript has [functional scope](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope), so the `secret` variable exists only within the scope of the function.

```
var iAmGlobal = true;
iAmGlobalToo = true; // no var made this an implied global

(function () {
  var iExistInThisFunctionsScope = true;
  iAmAnImpliedGlobal = true;  // omitted var attached this to the global scope
}());

```

Omitting `var` doesn't necessarily make a variable global. What happens is, when a variable isn't found in the current scope, the parent scope is checked. This happens either until the variable is found, or the `global` scope is reached. When you *accidentally* forget `var`, you often end up with an *implied global*.

[What are some of the problems of "Implied Global variables"?](https://stackoverflow.com/questions/4909578/what-are-some-of-the-problems-of-implied-global-variables)

Why use getters/setters?
========================

Using [closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures) to create getters/setters allows you to control how the variables are modified. Because in Javascript everything is mutable, anything can be changed by any other thing. This seems cool at first, but you soon realize that you have no control over who or what is changing your variables. Getters and setters allow you to **hide** the variable in the function's scope, so if something wants to change it, it must go through you.

Here's an example of controlling your variables.

```
function Foo () {
    var privateVar = 1234;

    this.get = function () {
        return privateVar;
    }

    this.set = function (x) {
        // privateVar doesn't exist in this function's scope,
        // but it will be found in the next scope up the chain.
        privateVar = x;
    }
}

// create an instance
var myFoo = new Foo();

// use the getter/setter
console.log(myFoo.get());  // 1234
myFoo.set(999);
console.log(myFoo.get());  // 999

// try to change privateVar outside
// of the getter/setter
myFoo.privateVar = 'not what you think';

// We didn't actually change the var.
console.log(myFoo.get());  // 999

// We added a new property.
console.log(myFoo.privateVar);  // 'not what you think'

```
Because it's defined within `(function() { ... })`. If you want to make it global, declare it alongside your `getValue` and `setValue`.

The `var` keyword simply declares a variable, it doesn't make it global:

```
var secret = "Global";        // Global variable declaration

function myFunction() {
    var secret;               // Local to myFunction()
    secret = "Hello, world!"; // Only local secret variable changed
    console.log(secret);      // "Hello, world!"
}

console.log(secret);          // "Global"

```

Dropping the `var` will only make it global if the variable has already been defined in the global scope:

```js
var secret = "Global";        // Global variable declaration

function myFunction() {
    secret = "Hello, world!"; // Global secret variable changed
    console.log(secret);      // "Hello, world!"
}

console.log(secret);          // "Hello, world!"

```