Created
February 14, 2018 18:12
-
-
Save 821jieun/7f18fb0c20853bd023629bcf7b12dd89 to your computer and use it in GitHub Desktop.
Challenge: in your own words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What is scope? Your explanation should include the idea of global vs. local scope. | |
Scope is the area in which a variable is available. | |
Local scope is when the variable is defined and used within a specific function. Local scope is re-created | |
each time the function is called. | |
Global scope is when the variable is defined outside of a function. | |
The interpreter follows a scope chain to check the availability of a variable. It will first check local scope; | |
then it will check parent scope; then it will check the global scope. | |
Why are global variables avoided? | |
Use of global variables can make a function indeterminate. This means that a when a function is given the same input | |
two different times, it outputs two different results instead of reliably mapping input to output. This makes it difficult | |
to reason and reuse the function. | |
Also, global variables make it difficult to debug errors since global variables can reach across multiple files. | |
Explain JavaScript's strict mode. | |
By putting the string 'strict mode' at the top of a file or a function, one can ensure that a variable cannot | |
be declared without using either the keyword 'let' or 'const'. This makes it impossible to accidentally create | |
a global variable. | |
What are side effects, and what is a pure function? | |
A side effect is any result of a function other than the returned value of the function. Functions can intentionally | |
have side effects, such as a function that changes the records of a database. However, unintended side effects | |
can make it difficult to reason about the function. | |
A pure function is a value-returning function that always returns the same value when called with the same arguments; it can | |
be replaced with the returned value of the function itself and everything can still run the same eway. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment