Created
March 14, 2017 03:16
-
-
Save leovolving/045b4f71a81008db76bad5c736446364 to your computer and use it in GitHub Desktop.
2.5.4
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 defines the area in which a variable either can or cannot be reached. For example, if I were to declare a variable | |
outside of a function, it would be considered a global variable and can be accessed/manipulated at any point thereafter | |
within my JS file or any JS files that follow it in an HTML file. If I were to declare a variable within a function, it | |
would be considered a local scope and can only be accessed or manipulated within that variable. | |
*Why are global variables avoided? | |
Global variables are generally avoided because they can have unwanted outcomes in the code. Because a global function can | |
be manipulated within any function within my code, the value of that variable can be unpredictable and even unreliable. The | |
variable would become indeterminant, meaning I would not be assured that, within a paricular function, my variable would | |
consistently provide the same outcome if given the same inputs. | |
*Explain JavaScript's strict mode | |
Strict mode is used to avoid altering a global variable unidential. If I include "'use strict';" at the the top of my JS | |
document, the JS file will throw an error any time a try to define a variable without using the "var" keyword. | |
*What are side effects, and what is a pure function? | |
Side effects happen any time a global variable is manipulated outside of the scope in which it was declared. A pure function is a | |
function which has no side effects and is determinant (typically, a function that does not contain global variables). | |
*Explain variable hoisting in JavaScript. | |
Variable hositing is the process in which the computer parses JS. All variables that get called prior to being declared | |
are hoisted to the top of the scope. However, the variable's assignment does NOT get hoisted with the declaration. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment