Created
November 1, 2016 15:32
-
-
Save nicholasshirley/a232e6b520d89e73753470c628b98bef 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. local scope. | |
Scope is the frame in which a variable lives. In JS there are 2 scopes (excluding ES6) global and local. Global variables are availabe to any function and can be called from anywhere (including other files, if loaded first) and local variables are only available inside their function. | |
Why are global variables avoided? | |
Global variables are more likely to have unintended side effects. Side effects are when a function reaches outside its scope to change a variable further up the scope chain and unintended indicates that the change is not wanted. If multiple functions are reaching out and changing the same global variable, they can become indeterminate (not produce the same result when run) and lead to bugs. | |
Explain JavaScript's strict mode | |
Strict mode, indicated by the 'use strict' at the top of the document (or inside a function), requires variables be explicitly declared before use. One benefit is that if a global variable would be created as a result of a function, it will instead throw an error. | |
What are side effects, and what is a pure function? | |
A side effect is a change in variables outside the scope of a function. A pure function is a function that is determinate and has no side effects (gives the same outputs with the same inputs every time and doesn't affect variables outside its function). | |
Explain variable hoisting in JavaScript. | |
When a JS file is first parsed, all variables are loaded into memory as undefined. If a variable is declared below a function call which requires it, for example, it will not return a reference error because the variable has in fact been created (hoisted) and given a value of undefined. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment