Skip to content

Instantly share code, notes, and snippets.

@dannydi12
Created October 3, 2019 19:40
Show Gist options
  • Save dannydi12/fc2789b6fae358f6406c7de4a8add0f8 to your computer and use it in GitHub Desktop.
Save dannydi12/fc2789b6fae358f6406c7de4a8add0f8 to your computer and use it in GitHub Desktop.
1) Scope is the area in which a variable is declared or referenced. I like to think of it as different classrooms in a school. There might be several students named John in a school, but we can reference a specific student named John based on which classroom they reside in. For example, I can be looking for the John in classroom 1B instead of just any other John in the school. Block and global scope work similarly: I can reference a specific variable that belongs to a function (block scope) or I can enlarge my scope of reference and find a variable higher up in the scope chain (global scope).
2) Global variables need to be avoided to prevent bugs and unintended side effects. It might be more convenient when you first start writing your code. However, it will quickly devolve into an unmanageable mess as your code starts to behave in ways you didn't intend (aka the AI uprising). Do you really want to be responsible for the end of humanity?
3) Javascript's strict mode can help you maintain more manageable code by forcing you to avoid global variables. If you try to reference a global variable under Javascript's strict mode it will result in an Uncought Reference error.
4) Side effects are unintended consequences as a result of improperly structured code. They can be avoided by following the methods described above. While we want to avoid side effects, we want to strive for pure functions. This means that the function doesn't cause any side effects and is determinate. An impure function would be indeterminate and unpredictable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment