Skip to content

Instantly share code, notes, and snippets.

@mikeymaio
Created November 13, 2016 05:45
Show Gist options
  • Save mikeymaio/d239433a8df9b6ef0d4152e5d76e312c to your computer and use it in GitHub Desktop.
Save mikeymaio/d239433a8df9b6ef0d4152e5d76e312c to your computer and use it in GitHub Desktop.
Unit-2-Lesson-5-Questions
What is scope? Your explanation should include the idea of global vs. local scope.
Scope is a heirarchy for access to variables. Javascript has two scopes: global and local.Global scope means for variable can be
accessed everywhere in the code. Local scope, however, limits the use of a variable to the function in which it was created. When a
variable is referenced, the Javascript interpeter will begin looking locally and move toward global scope until it finds the defined
value. If it reaches the global scope and cannot find a defined value, it will raise an error
Any variable that is declared outside of a function has global scope.
Why are global variables avoided?
Global variables are avoided as a way to keep programs determinate. Local variables cannot be altered from outside of their function,
but global variables can, which can cause the program to behave unexpectedly.
Explain JavaScript's strict mode
Strict mode is kind of like a safety net. Instead of allowing 'bad syntax', it will raise errors for code that may be problematic.
For example, using a variable or object without declaring it will raise an error. Mistyping a variable name would throw an error in
strict mode, whereas, without strict mode, a new global variable would be created.
What are side effects, and what is a pure function?
A side effect is when a function reaches outside of its scope and alters something. At times, this can be useful, but only when it
is intentional.
A pure function is one without any side effects that will return the same value every time it is given the same input.
Explain variable hoisting in JavaScript.
When the Javascript interpreter runs code, it makes to passes. During the first pass, it sets aside memory for declarations of
variables and functions and moves those declarations to the top of their scope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment