Skip to content

Instantly share code, notes, and snippets.

@alfaraday
Created October 28, 2016 00:44
Show Gist options
  • Save alfaraday/1be9afda576c5201da6e86850ef0cd3a to your computer and use it in GitHub Desktop.
Save alfaraday/1be9afda576c5201da6e86850ef0cd3a to your computer and use it in GitHub Desktop.
o What is scope? Your explanation should include the idea of global vs. local scope.
Scope refers to where in your code a variable can be accessed, and it depends on where a variable is declared. Global variables are declared outside of a function, and can therefore be accessed anywhere in your code, including in separate files. Local variables are declared within a function, and can only be accessed within said function.
o Why are global variables avoided?
Global variables can lead to unintended side effects that turn code indeterminate – the same set of instructions with the same set of inputs will return different outputs.
o Explain JavaScript's strict mode
By using the ‘use strict’ command at the top of a file, you’ll get an error when a variable is declared without the var keyword. That prevents you from creating a global variable unintentionally either through typo or forgetting to declare it locally.
o What are side effects, and what is a pure function?
Side effects are when a function alters a variable that’s outside of its scope (i.e. not contained within the function itself). A pure function’s return value is based entirely on its inputs. Pure functions don’t alter any variables outside of their scope. They are determinate – the same set of instructions, given the same inputs, will always return the same output.
o Explain variable hoisting in JavaScript.
When a script is loaded, the interpreter scans the code for variables (and functions) before executing. Variables are moved up to the top of their scope (top of the script for global variables, top of the function for local). This is only true for the declaration of a variable and not the assignment of its value – the interpreter knows ahead of time that the variable exists, but not necessarily what its value is.
@jamesduncancss
Copy link

nice work!

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