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.
Written by Todd Wright
Accelebrate’s training classes are available for private groups of 3 or more people at your site or online anywhere worldwide.
Don't settle for a "one size fits all" public class! Have Accelebrate deliver exactly the training you want, privately at your site or online, for less than the cost of a public class.
For pricing and to learn more, please contact us.Contact Us
New York City
Salt Lake City