Guest User

Untitled

a guest
Jul 18th, 2019
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. What is scope?
  2. Coming from the medical field, I am able to liken the idea of JavaScript scope to the scope of practice. As an immediate responder you had to recognize when things were outside of your scope of practice. When you didn't have the training and/or knowledge you had to look outside of your scope to get the information and/or treatment needed. Synonymously, the JavaScript scope chain does much of the same when looking for what it needs to complete it's local block of code. The only difference is there is no variable shadowing in the medical field as the global, in that case, would take precedence instead of in JavaScript where the local variable takes precedence. Getting into JavaScript scope specifically, the difference between block scope and global scope is that when a variable is declared inside of a function, it is only accessible inside of the function walls, which is called BLOCK scope. Variables declared outside of a function have a GLOBAL scope since they can be accessed from anywhere in the code.
  3.  
  4. Why are global variables avoided?
  5. They are avoided because of the unintended side effects of when a function reaches outside of its local scope and alters the value of the global variable. The problem is because other blocks of code may be relying on that global variable to remain constant. When this happens it can create indeterminate functions, where they return different values each time which makes debugging much more of a messy chore.
  6.  
  7. Explain JavaScript's Strict Mode.
  8. 'Use Strict' at the top of a JavaScript file forces you to declare variables using let or const. This alleviates the issue of JavaScript creating a new variable in the global scope or mutating it if it already exist globally. If you declare a variable without using let or const you will receive a error message in return as a reference error.
  9.  
  10. What are side effects, and what is a pure function?
  11. A side effect is when a function reaches outside of its block and alters the value of a global variable. A pure function is one that is determinate and has no side effects. A determinate function is one that always returns the same value when given the same inputs.
RAW Paste Data