../blog/t6-scope

Ruby Explains: Scope

Basically, scope in JS can go either of two ways - local scope or global scope. If you're writing code, you will want to declare your functions to be local or global - e.g. if you want your variables to be visible from outside the function, or if you don't. If you assign a value to the variable that has not been declared, it automatically becomes a global function, which is important to remember. If you want to keep the variable local, then you have to declare them within a function.