Guest User

Untitled

a guest
Oct 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. What is scope? Your explanation should include the idea of global vs. local scope.
  2. Scope determines the accessibility of variables in the program, what variables we have access to from different parts
  3. of the program. Javascript has two scopes: local scope and global scope.
  4. Variables declared in global scope are visible and accessible to all programs.
  5. Variables declared within a function or within block of code (code between curly brackets)
  6. are local to that function or block of code. Those variables are only visible within their local scope
  7. (function or block of code), and they are not accessible from the global scope.
  8.  
  9. Why are global variables avoided?
  10. Global variables should be avoided because they can make code unpredictable and produce unexpected results.
  11. For example, if first function relies on the global variable to properly work, but the second function changes
  12. that global variables, than the first function would brake.
  13. When the program has many functions that rely or change global variables, the results such program produces
  14. could be unreliable and it would be very hard to find the cause of such unfavorable behavior.
  15. Therefore it is better to use local variables, and use global variables only if it is your intention to change them.
  16.  
  17. Explain Javascript’s strict mode.
  18. Strict mode is a command (“use strict”; ) that is placed at the beginning of a script.
  19. This mode makes code more secure, by not allowing to use variables without declaring them first.
  20. Therefore it is not possible to accidentally create global variables in strict mode.
  21.  
  22. What are side effects, and what is a pure function?
  23. A function should work as it is intended to. For example, we have a function that produces the sum of two numbers
  24. that were passed into it. When we pass in 1 and 1 the result should be 2, no matter how many times we pass
  25. the same numbers the result should be the same. The pure function always produces same result given same parameters.
  26. When the same function instead of expected result of 2 will give us every time different number, such function
  27. is not pure and is not working as it should, because it relies on global variable and at the same time with each
  28. invocation changing that variable and thus producing side effects.
  29. Side effects are when a function unintentionally modifies global variable.
Add Comment
Please, Sign In to add comment