Skip to content

Instantly share code, notes, and snippets.

@dhoel
Created December 28, 2016 23:37
Show Gist options
  • Save dhoel/08b0ac9421eca0129e3849e97f2d6325 to your computer and use it in GitHub Desktop.
Save dhoel/08b0ac9421eca0129e3849e97f2d6325 to your computer and use it in GitHub Desktop.
What is scope?
> Scope, or Variable Scope, is the idea regarding in which part of a program a given variable can be referenced or changed.
In Javascript there are two scopes, Global and Local. Local scope is within a function so any variable that is declared
(var varOne;) within a funtion can only be referenced or changed from inside that function - that variable's scope is local
to that function. Global scope is throughout a program, outside of functions as well as within any functions. A variable
that is declared outside of any functions has global scope and can be referenced or changed in any part of the program. Also,
when a variable is assigned a value without being specifically declared (varOne = 'value';) from inside a function, that variable
has global scope as well.
Why are global variables avoided?
> Global variables are best avoided to prevent unintended side effects, when code inside a function effects code outside of that
function's local scope with unintended results. This can lead to code that will give different results for the same inputs at
different times.
Explain JavaScript's strict mode.
> Using strict Mode ('use strict';) will cause an error to be triggered if a variable is created without specifically declaring
it using the keyword "var" (e.g. [varOne = 'foo';] instead of [var varOne = 'foo';])
What are side effects, and what is a pure function?
> Side Effects are when code in one function effects code outside of it's local scope, in another function, by changing values
in that other function. Sometimes side effects are intended but when they are unintentional they can cause indeterminate code, or
code that will give different results for the same inputs. A pure function is a function that has no side effects and is a
determinate function, always giving the same results for the same inputs.
Explain variable hoisting in JavaScript.
> Variable hoisting refers to how Javascript reads variable declarations when parsing the code before execution. Javascript interprets
a variable declaration and value assignment together (var varOne = 'foo';) as 2 statements, 1. the declaration and 2. the value
assignment. The Javascript interpreter will set aside memory for the variable and leave it as undefined, or basically the interpreter
is moving, or hoisting, all the variable declarations in a given function to the top of that function prior to executing the code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment