Skip to content

Instantly share code, notes, and snippets.

@asw001
Created May 3, 2017 03:06
Show Gist options
  • Save asw001/4f88be045277e935b7d72ff4e17d9460 to your computer and use it in GitHub Desktop.
Save asw001/4f88be045277e935b7d72ff4e17d9460 to your computer and use it in GitHub Desktop.
scope, hoisting, side-effects
What is scope? Your explanation should include the idea of global vs. local scope.
Scope refers to the conditions under which a JavaScript program can access variables. There are two
types of scope in ECMAscript version 5: local and global.
Global variables are globally accessible, which means that they are accessible anywhere within a program. Contrasted
with global variables, local variables are accessible only within a function.
Variable precedence begins with local variables, located in functions, then moves outward to global variables contained
in the larger program; the implications of this are that if a variable were to be declared inside and outside of function,
the variable inside the function would override the value of the global variable. This behavior is called 'scope chain'.
Why are global variables avoided?
To avoid confusion with variable precedence, and other unintended side effects, it is best to not use global variables.
However, there may be instances where it may make sense to declare a global constant, such as the value of Pi; this value
is only referred to, and will not change. For a certainty, however, an exception to the rule of not using global variables is using a JavaScript library.
However, in all other instances, global variables are best avoided to prevent the values of variables defined in the global scope from unintentionally, or unexpectedly, changing;
best practices state that this can be avoided by passing values to functions. This protects the values of variables by confining them to local scope.
Explain JavaScript's strict mode
JavaScript's 'strict mode' ensures that not using the 'var' keyword produces an error, e.g. 'Uncaught ReferenceError: foo is not defined'. The use of 'var' for all variable declarations prevents the unintentional mutation, or override of variable values. This is true for the scope chain as well as variables used within the same scope.
What are side effects, and what is a pure function?
A side effect occurs when a function follows the scope chain outside of itself to alter a global variable; the function is altering a variable with the same name as defined within the function.
A pure function is a function which:
* Given the same input, will always return the same output.
* Produces no side effects.
* Relies on no external mutable state.
it follows that for any variables manipulated within the function, the function should return a value.
-- https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076bec976
Explain variable hoisting in JavaScript.
Variable hoisting is the behavior of JavaScript whereby the interpreter will locate every variable within a scope and move it to the top of the scope.
This behavior has the effect of variable instantiation based on the first appearance of variable with a scope; hoisting will, if the variable has not been defined, implicitly assign the
undefined type to variable.
For example,
function myFunc2() {
console.log(myString);
var myString = 'foo';
}
expands to
function myFunc2() {
var myString; // undefined
console.log(myString);
var myString = 'foo';
}
This will cause the function to print 'undefined' to the console, which is likely not what is intended.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment