Guest User

Untitled

a guest
May 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. What is scope? Your explanation should include the idea of global vs. local scope.
  2.  
  3. The concept of scope is simple; it refers to the accessibility and visibility of variables in your code. When defining a variable, where you define it on the scope matters and will cause different outcomes depending on where it is. If it is outside of a function and stand alone, essentially, that variable becomes what is considered global. When a variable is located on the global scope (not nested in a function), it is accessible from anywhere else in the code - even from different files! This can cause a number of issues when the same variable name is used, so it’s better to avoid these entirely and always define variables lower down the scope chain (and never forget to use let or const).
  4.  
  5. Alternatively, there is local scope. When a variable is defined in a local scope, that means it is nested in a function and it’s definition only rings true whilst still running in that function. Ergo, variable (x) could have one definition for functionA, and another variable (x) could have an entirely different definition for functionB. Because they are located on local and not global scopes, the definitions will never collide or cause any issues.
  6.  
  7. Why are global variables avoided?
  8.  
  9. There are many problems that can come along with global variables, the two most notable being the case of side effects and a function rendering indeterminate. When side effects occur, a function travels outside its own scope and alters something higher in the scope chain. This, in turn, can alter the entire code and cause other functions to misbehave. An indeterminate function is one that might return different values each time it’s run, usually due to side effects which are the result of global variables. In order to avoid any headaches with your code - and unnecessary ones, at that - it’s best to avoid global variables as a whole.
  10.  
  11. Explain JavaScript's strict mode
  12.  
  13. JavaScript’s strict mode is an essential tool when it comes to avoiding issues or bugs in your code. One of the main features that stuck out to me was the fact that it will trigger an error if let or const are skipped, which is an easy way to accidentally create a global variable. Instead of letting you continue to code, strict mode will let you know then and there that something was missed so that you can easily fix it before it gets lost in your code and you have to comb through it for the error you’ve made.
  14.  
  15. What are side effects, and what is a pure function?
  16.  
  17. Side effects are when a function reaches outside of it local scope higher up into the scope chain (a parent or a parent’s parent, etc) and alters something that isn’t necessarily in its jurisdiction. On the other end of the spectrum, a pure function is a function that is determinate and has no side effects. A determinate function means that it will return the same value each time that it’s given the same inputs, making it predictable.
Add Comment
Please, Sign In to add comment