Skip to content

Instantly share code, notes, and snippets.

@michaelablackham
Created April 17, 2017 02:46
Show Gist options
  • Save michaelablackham/19e4b920c0671fd02ab464409ba437a6 to your computer and use it in GitHub Desktop.
Save michaelablackham/19e4b920c0671fd02ab464409ba437a6 to your computer and use it in GitHub Desktop.
1. What is scope? Your explanation should include the idea of global vs. local scope.
- Scope is the way the the browser parses the information on the page. It is like a backward ladder that first looks
within the function, then steps back and looks within the parent, etc.
- global scope is a variable that is defined within the body or outside of a function and is then avaiable everywhere
within your site
- local scope is a variable defined within that current function. This can be the same variable from the body, but is
only relevent to that current function
2. Why are global variables avoided?
- Global variables should be avoided so that no unintended side effects happen within the code. This makes it available
everywhere in the code, which then becomes a bit difficult to maintain.
3. Explain JavaScript's strict mode
- javascripts strict mode is a great way to avoid any kind of weird global variables. If "var" is not included when
defining a variable, it will trigger an error instead of assigning this variable definition as a global variable. One
piece i found interesting with this is that it can be included at the top of the code or within a function.
4. What are side effects, and what is a pure function?
- A side effect is when the code reaches up the chain to a parent or body variable and changes the value within that part
of the function.
- A pure function is when there are no side effects
5. Explain variable hoisting in JavaScript.
- Variable hoisting is the way that the browser parses the information (variables, functions etc) within the code.
It quickly runs through all the of the code and logs what it finds within it's "memory". This enables the browser
to find all the variables, and know that it needs to make space for it and its definition
@joeljuca
Copy link

Good answers! :)

Hoisting is also useful for defining functions at the bottom of a file and have it available everywhere - including before its definition. Ex:

// running helloWorld - even when it's only defined later in this file
// thanks to JS hoisting!
helloWorld()

function helloWorld () {
  console.log('Hello, cruel world!')
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment