Last active
July 18, 2019 08:37
-
-
Save psychicbologna/b206bd6a451d49cc876aa0a340730e86 to your computer and use it in GitHub Desktop.
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. block scope. | |
Scope allows Javascript to define how a variable is or isn't accessed and mutated by code in functions based on how specifically it is | |
designated in the block chain. It searches for the value for a variable name from within the current scope outward to parent scopes | |
until it reaches the global level. This prevents variables with the same name but different values set within functions from colliding | |
with each other and breaking the code by giving block scope precendence over global scope. | |
When setting variables using let and const, there are two kinds of variable scopes in Javascript: global and block. A global variable is | |
declared outside of a function. It is available everywhere in the code and even between files if they are called in proper order. A | |
block variable is declared within a function, and is only accessable locally to the function in which it is declared. | |
- Why are global variables avoided? | |
Although sometimes useful, the values of global variables can be changed by block variables reaching outside their intended scope, and | |
vice versa. This is especially true if a lot of code has already been written relying on problematic globals, making the source of the | |
problem harder to find. | |
Explain JavaScript's strict mode | |
When placed at the top of a Javascript file, strict mode prevents global variables from being written without let and const, throwing | |
errors as if the offending variable were never declared. This allows you to find the problem variable and keep the function from | |
unintentionally creating a global scope variable, or mutating it if it already exists. | |
- What are side effects, and what is a pure function? | |
Side effects are changes from one scope upwards to others in the chain. This has useful applications, such as allowing functions to | |
change information in databases. However, unintentional side effects can create complications that change how a variable is interpreted | |
on global and block scales. | |
A pure function is free of unintended side effects and determinate. When determinate, it will always produce a consistent value given | |
the same input. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment