Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. What is scope? Your explanation should include the idea of global vs. block scope.
  2. The scope is what controls the accessibility of all of the variables, functions, and objects within your code. This accessibility is defined as how and where variables are run throughout the code. Global scope is a variable that is defined outside of a function. Because a global scope is defined outside of a function, that variable is applied to the code in its entirety. Global scope’s counterpart is block scope. Block scope is unlike global scope in that it only applies to certain parts on the code because the variables are locked into that specific functions instructions.
  3.  
  4. Why are global variables avoided?
  5. The use of global variables can lead to maintenance concerns down the line when it comes to the code. This leads to more bugs and therefore more work for developers. It also makes it harder to collaborate with other developers. This is because of side effects where part of the global variable may reach outside of itself and alter another, unintended, part of the code and therefore changes that parts values. Another problem that may arise when you are using global variables is indeterminate inputs. Instead of a variable expressing in a way that has the same effect every time, it may result in returning one value in part of the code and then a completely different value in a separate part of the code. This makes it impossible to track bugs down.
  6. Explain JavaScript's strict mode
  7. Strict mode was introduced in order to make current browser code compatible with older code. It is recommended to put ‘use strict’ at the top of javascript files because it makes it harder to accidentally create a global variable. This is because if a global variable is accidentally created in strict mode then an error message will play back. In order to invoke Strict Mode, you place it at the top of your document or function and use the “use strict” directive.
  8. What are side effects, and what is a pure function?
  9. A side effect occurs when a function oversteps its own local scope up to the parent scope and then proceeds to change the value within that parent function. A combination of a global variable and a side effect can produce code that is indeterminate. We don’t want our code to be indeterminate because we always want our code to return the same value every time that function is run. Indeterminate produces code that changes up the value and therefore is not cohesive. As developers we want our code to have pure function. A pure function is where a function is both determinate and has no side effects. Essentially, a pure function stays in its lane and does what it needs to do where it was predestined to be.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement