Skip to content

Instantly share code, notes, and snippets.

@goldtreefrog
Created October 20, 2017 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldtreefrog/731ca64cedb888007f81a59e4355c4a5 to your computer and use it in GitHub Desktop.
Save goldtreefrog/731ca64cedb888007f81a59e4355c4a5 to your computer and use it in GitHub Desktop.
Scope and the Problem with Globals Q&A
What is scope? Your explanation should include the idea of global vs. local scope.
A: Scope refers to where a variable gets and retains its value and definition. A variable defined within a function only retains its definition and value within that function (and within subfunctions of that function). A variable defined globally is accessible and changeable everywhere, but if a variable with the same name is defined locally within a function, that local variable takes precedence while within that function - and disappears outside the function, while the global variable remains.
Why are global variables avoided?
A: Unexpected results are more likely to happen because a variable such a variable may be used by more than one function, and changing its value from within one function will change it for other functions as well. If that was not the intention, bugs will happen.
Explain JavaScript's strict mode
A: JavaScript's "use strict" statement causes JavaScript to issue an error any time a variable is not explicitly declared before it is used. This helps catch potential errors in scope.
What are side effects, and what is a pure function?
A: A side effect is the result of using a variable that is defined up the scope chain, causing the variable to be changed for another function in a way that is unexpected if it was unintended. A pure function is one that always gives the same result when given the same input and that creates no side effects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment