Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. What is scope? Your explanation should include the idea of global vs. block scope.
  2. Scope is about which parts of the application/program have access to a variable. If a variable is defined outside of a function, it is considered to have global scope. This means that any file within the application can access and alter the variable. In constrast, a variable that is defined within a function is considered to have block scope. This means that the variable can only be accessed and altered within the function itself. After the function execution has completed, the variable essentially disappears as far as the rest of the program is concerned.
  3.  
  4. Why are global variables avoided?
  5. Global variables should be avoided because they introduce the potential for a program to have unintended behavior at runtime. Since variables with a global scope can be accessed and altered from any file within the application, one part of the program may alter a global variable that a second part of the program is dependent on. In this case, the output of the second function may not be what the developer was expecting. Both discovery and prevention of this bug would be difficult due to the use of a global variable.
  6.  
  7. Explain JavaScript's strict mode
  8. JavaScript provides developers with the option to force JavaScript files and functions to use strict mode. This is denoted by placing "'use strict';" at the top of the file or above a function. Any file/function with this command will throw an "Uncaught ReferenceError" exception for any variables that are declared without the "let" or "const" keyword. This essentially removes the option for a developer to write a function that is dependent on global variables by forcing all variables to have block scope.
  9.  
  10. What are side effects, and what is a pure function?
  11. Side effects are changes made outside of the local scope. A pure function is a function that will always return the same output when given the same input (they are determinate). Pure functions also cannot have side effects.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement