Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. What is scope? Your explanation should include the idea of global vs. block scope.
  2.  
  3. According to Merriam-Webster, scope is a range of operation. In Javascript,
  4. scope defines the range of operation of declared variables and more importantly our access to these variables
  5. dependant upon where in the code they were declared. Variable scope are rules defining from which parts of your code
  6. you can access a particular variable.
  7.  
  8. Global scope refers to the entire Javascript program, application or files.
  9. Variables declared in the global scope can be accessed from anywhere in your code, inside and outside of functions.
  10. A variable with block scope means the variable is only accessible from within the function’s block of instructions.
  11. When the function is invoked, the variable is called and when the function is done executing, the variable disappears.
  12.  
  13. Which leads to, why should global variables be avoided?
  14.  
  15. Global variables should be avoided because they make unintended side effects in a program more likely.
  16. This usually happens when a function reaches outside its block scope into the global scope to access and/or alter a value
  17. that lives there. This almost guarantees that code becomes indeterminate, meaning when given a single set of inputs
  18. it returns one value one time and a different value another time. We want determinate code that will always return the
  19. same value given the same inputs. Indeterminate code leads to hard to find bugs and frustration.
  20.  
  21. What is Javascript’s strict mode?
  22.  
  23. Strict mode makes it easier to write “secure” Javascript. With strict mode enabled, a variable must be declared
  24. with let or const or an error will be triggered. Also, in JavaScript without strict mode enabled,
  25. a mistyped variable name creates a new global variable. In strict mode this produces an error and makes it
  26. impossible to accidentally create a global variable.
  27.  
  28. What are side effects, and what is a pure function?
  29.  
  30. Side effects are usually unintended and occur when a function reaches out of it’s local block scope into the parent or
  31. global scope and accesses or alters a value that lives there. This leads to indeterminate code which doesn’t
  32. consistently return the same values given the same inputs. When code is determinate, consistently returning the same values
  33. for the same inputs and has no side effects, this is considered pure function. Pure functions are easier to read,
  34. easier to debug, easier to test and operate consistently.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement