Guest User

Untitled

a guest
Feb 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. Questions
  2.  
  3. 1. What is scope? Your explanation should include the idea of global vs. local scope.
  4.  
  5. Scope is determined by where a variable is accessible within your code. If a variable is defined outside of a function, it can be accessed everywhere, and is considered to have a global scope. Global scope variables can be accessed in multiple functions, and sometimes in different JS files depending on if the file with the global scope is loaded before the others. A variable that has a local or block scope is defined within a function, and is only accessible within it's respective function. Once the function that defined this variable is finished running, the variable is no longer needed, and therefore disappears, its data is no longer needed until the next time the function has been called.
  6.  
  7. 2. Why are global variables avoided?
  8.  
  9. Global variables are to be avoided because they can have unitended side effects on the results your code delievers. As an example, if you set a global variable to a specific value, and your function sets that variable to a new value, istead of disposing of that value once the function has completed, the new value is saved. If a different function now attempts to use the same variable, the value will have changed. This can cause problems in complex scripts as it will not throw an error, but will cause your results to be incorrect.
  10.  
  11. 3. Explain JavaScript's strict mode
  12.  
  13. When Strict mode is used it makes sure that no variables are declared without the let or const keywords. If one is declared without using let or const keywords an error will be thrown. Strict mode can be used for the entire file, or can be put at the top of a function to use strict mode on just that function.
  14.  
  15. 4. What are side effects, and what is a pure function?
  16.  
  17. A side effect is when a function reaches outside of its local scope into a parent scope or global scope to reach the value of a variable and then changes this variables value. This can cause unitended propblems on other fuctions that may utilize the same variable. A pure fuction is a function that does not have any unitended side effects and is considered determinate. Determinate means that a function will always produce the same result with the same inputs given. An inditerminate function with the same given input may deliever one result some times and a different result at other times.
Add Comment
Please, Sign In to add comment