http://jsbin.com/verovok/edit?js,console,output
Scope is the part or section of a program to which a given function, variable, or object has access. Global scope is accessible by any and all functions, while local scope is only accessible to the encasing function and any function(s) inside said encasing function.
Global variabes are to be avoided for various reasons. One being they pollute the namespace, i.e. you may use a global variable accidentally when intending to use a local one. They are also difficult to track: similarly to the point above, you may forget about the global variable if your program gets very large and accidentally modify it without intending to. Furthermore, they make it more likely to have unintended side effects pop up in your code, creating a codebase that is difficult to test, debug, and develop.
https://jsbin.com/muqiyoq/edit?js,console
https://jsbin.com/yukusiw/edit?js
(Keep in mind that I wasn't able to do these first two on my own and basically copied the answers from the solutions.)
https://jsbin.com/waxopar/edit?js,output
https://jsbin.com/gozuvo/edit?js,output
http://jsbin.com/sojaca/7/edit?js,output
(for the last one, I needed to look at the solution. This is as far as I got: http://jsbin.com/sojaca/4/edit?js,output)