Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. 1. What is scope? Your explanation should include the idea of global vs. block scope.
  2.  
  3. Scope is the range at which the entire code can access your variables. Global scope means that a variabe can be accessed
  4. and referenced throughout the entirety of the code. Block scope means that a variable can only be accessed in certain
  5. "blocks" of your code - i.e. inside a function. If it shares the same name as a variable outside the function (global),
  6. it will have a different value inside the function.
  7.  
  8. 2. Why are global variables avoided?
  9.  
  10. Global variables are avoided mostly because they create bugs and unintended side effects throughout the code, that may
  11. become very difficult to find and fix later on. It can alter varaiable values outside of functions, and change how the
  12. values are read throughout the code. It makes code become "indeterminate", which means that functions may not always
  13. return the same value when provided with the same inputs.
  14.  
  15. 3. Explain JavaScript's strict mode
  16.  
  17. JavaScript's Strict Mode will return an error message whenever a variable is created without using "let" or "const".
  18. This is important, because variables created without these parameters may return mutated values throughout the code.
  19. Using Strict Mode is considered best practice when writing in JavaScript, unless there is a specific reason not to do so.
  20.  
  21. 4. What are side effects, and what is a pure function?
  22.  
  23. Side effects in code refers to when a function reaches outside its own boundaries and starts effecting the rest of the
  24. code on a "global" scale, which can start to alter the values of other variables, potentially causing bugs to appear.
  25. This will cause code to become indeterminate, not always returning the same value, again leading to the creation of
  26. bugs. A Pure Function is the opposite of this - it is when the function is both determinate (always returning the same
  27. value), and causes no unintended side effects (the function follows block scope) and, like strict mode, it is
  28. considered best practice to always write pure functions.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement