Created
April 18, 2017 18:36
-
-
Save wilm42/72a6f225e3e55ea41815e9b4693afb58 to your computer and use it in GitHub Desktop.
answering questions related to scope and global variables
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
Q1: What is scope? | |
'Scope' refers to the places that a variable can be called on in javascript. A variable with 'local scope' is one that | |
is defined within a specific function, and can only be used within that function. If it is used elsewhere without being | |
redefined, you'll get an uncaught ref error. A variable with 'global scope' is one that is defined outside of a function. | |
variables with global scope can be called on anywhere in your code, even between files. | |
Q2: Why are global variables avoided? | |
Global variables are to be avoided because they cause a lot of bugs. The reason they cause a lot of bugs is because you can | |
accidentally mutate them in a function, and then when you call on them again later, the mutated data held by the variable | |
is not what you were expecting. By keeping your variables local, you're creating a variable and then directly working with it, | |
not mutating something that you may have written further up in your code or even in another file. | |
Q3: Explain JavaScript's strict mode | |
Another way (beside just typing it outside of a function) to create a global variable is by omitting the var keyword, even | |
inside of a function. JavaScript's strict mode does not allow this to happen, if you name a variable without the var keyword | |
and then reference it, you will get an uncaught ref error. this forces you to use the var keyword. | |
Q4: What are side effects, and what is a pure function? | |
If, in a function, you set a value to a variable that is not contained within that fucntion, you've created a side effect. | |
If you do this by accident, you've created an indeterminate function. A function is pure when it is both Determinite (i.e. | |
not indeterminate) and free of side effects. Pure functions should be the standard unless you specifically need one that | |
is not pure. | |
Q5: Explain variable hoisting in JavaScript. | |
When a js interpreter reads over your code, it does so in two passes - on the first pass, it will look for any variable | |
declarations and set aside space for them in it's memory. It does not, however, set a value to your variables until the | |
second pass - instead, it defaults to undefined. the term 'hoisting' refers to how the interpreter seems to 'hoist' | |
your variable declarations above anything else in a block. by this property, you can reference varibles that you haven't yet | |
declared in your function, though their value will be undefined. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment