Global scope are all those variables that are defined at the top level of your code.
The msg variable above is available everywhere in the page. The global scope is incredibly powerful for sharing variables around (remember, variables point to things: values, functions, and objects objects). The danger that everyone speaks about is the pollution of that namespace. There is only one global scope, and once you add a variable to that scope, another definition of that variable in the global scope is a collision.
Local scope is a set of variables that are available within a smaller set of code lines – generally a top-level function. Variables defined within a local scope are available to that narrower scope, but not the outer scopes.
Keep an eye out for a few more articles that will dive much deeper into the “Why” and patterns around this scoping topic.
Author: Todd Wright, one of Accelebrate’s instructors