Guest User

Untitled

a guest
Nov 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. What is scope?
  2.  
  3. The scope of a variable is region in which that variable is accessible. In Javascript, there are two regions: global and local.
  4. Variables with a global scope are accessible (and can be mutable, depending on their declaration) by all functions. This can
  5. even include functions in other files.
  6.  
  7. Variables with a local scope, by contrast, are declared within a function, and are only accessible within that function.
  8.  
  9. ----
  10. Why are global variables to be avoided?
  11.  
  12. A variable that can be accessed anywhere is prone to unwanted side-effects. Functions, generally, ought to be determinate. That
  13. is, if the input to a function remains the same, its output should as well. If a function consumes a global variable--one which
  14. can be used and changed anywhere--its output could be different given the same input on different occassions, if that global
  15. variable's value changed.
  16.  
  17. While this can sometimes be desirable, it can lead to unpredictable and unwanted results. This is especially true as a program
  18. grows in size and complexity. It is therefore good practice to give a variable the smallest scope needed to do its job.
  19.  
  20. ----
  21. Explain Javascript's strict mode
  22.  
  23. Javascript's strict mode raises an exception when a variable is declared without 'let' or 'const'. The purpose is to limit
  24. unwanted-side effects of manipulating global variables.
  25.  
  26. ----
  27. What are side-effects? What is a pure function?
  28.  
  29. Side-effects are changes a function makes outside of itself--changing the value of a global variable, for example. A pure
  30. function is is determinate (there can be no change in output without a change in input) and has no side-effects.
Add Comment
Please, Sign In to add comment