Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. What is scope? Your explanation should include the idea of global vs. local scope.
  2.  
  3. Scope refers to how variables behave within a file. If you declare a variable outside of a function, then it is considered a global
  4. variable. A global variable will be used on any part of the code as long as that same variable isn't used inside of a section of code.
  5. For example:
  6.  
  7. let x = 'optimusPrime'; //global variable
  8.  
  9. function(megatron) {
  10.  
  11. console.log(x); //since x isn't stated in any way in this function, when megatron is ran, it will display 'optimusPrime'.
  12. }
  13. If a variable isn't declared within a function, the computer will look up the scope chain to find if the variable has been declared
  14. somewhere else.
  15.  
  16. Why are global variables avoided?
  17.  
  18. Globals are avoided because they can create side effects to your code as well as change variables from the local scope to the parent scope.
  19. This leads to indeterminate situations.
  20.  
  21. Explain JavaScript's strict mode
  22.  
  23. Strict mode = 'use strict' and can be put at the top of any file to affect the entire file or put within individual functions to use
  24. strict mode inside that particular function only. Strict mode means that any variable that is issued without let or const before it
  25. will trigger an error.
  26.  
  27. What are side effects, and what is a pure function?
  28.  
  29. Side effects is when a function reaches outside of the local scope to the parent scope and alters a value that lives there. A pure
  30. function is a function that doesn't have any indeterminate values and doesn't have any side effects.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement