Question: In Javascript, if I declare variables with var in javascript outside of a function

```
var foo = 1;
var bar = 2;

function someFunction() {
    ....
}

```

are those variables within the scope of the document or the window? Furthermore, why does this matter? I know that if one declares a variable without var, then the variable is global.

Is there a simple way for me to test whether a variable falls within the scope of the document or the window?

Answer: 
```
var foo = 1;

window.foo === foo;

```

JavaScript is a functional language and therefore any variable declared inside the scope of a function is only available in that function.

JS will actually go through each functions scope and look for a variable declared.

```
function setGlobal() {
    bar = 1; // gets set as window.bar because setGlobal does not define it
}
setGlobal();

// logs true and 1
console.log(window.bar === bar, bar); ​

```

<http://jsfiddle.net/kXjrF/>

So...

```
function logGlobal() {
    var bar;
    console.log( foo, window.foo ) // undefined, undefined
    function setGlobal() {
        // window.foo is now set because logGlobal did not define foo
        foo = 1;  
        bar = 2; // logGlobal's bar not window.bar
        function makePrivate() {
           var foo = 3; // local foo
           console.log( foo ); // logs 3
        }
        makePrivate(); // logs 3
    }
    setGlobal();
    console.log( foo, window.foo ); // logs 1, 1

}

```
as JavaScript has only function scope, variables defined by the `var` keyword are scoped to the containing function.

you can easily check for global scoping by opening the JavaScript console in the browser (or directly in your code), and typing:

```
varRef && console.log(varRef)

```