Skip to content

Instantly share code, notes, and snippets.

@mikedolan03
Last active January 15, 2018 22:16
Show Gist options
  • Save mikedolan03/2b23f7ffcebf37a43d2d6431fbc31d26 to your computer and use it in GitHub Desktop.
Save mikedolan03/2b23f7ffcebf37a43d2d6431fbc31d26 to your computer and use it in GitHub Desktop.
1. What is scope? Your explanation should include the idea of global vs. local scope.
I like to think of scope as the distance variables can travel within a program. If a variable has global scope is can travel anywhere and be accessed and changed in any function in the program. Global variables can also be accessed and mutated in subsequently declared javascript files. A locally scoped variable is prevented from being accessed beyond the function it was declared it. Local variables values can still be accessed from the outside by passing the value as an argument in a function -- this way the local var is safe from being changed by external functions or even external js files.
2. Why are global variables avoided?
Global variables have the allure of easy access and make it possible to write functions without passing in arguements or returning anythings because you can just change the global variable. However, this is a dangerous way to program because any new code added or external js files might unknowingly reference the global variable and mutate it. The problem with this is that if such a mutation is unexpected, it turns program functions into indeterminate functions. Indeterminate functions mean that you can't count on the function returning the same value even when the input is the same because the global variable might be mutated from the outside.
3. Explain JavaScript's strict mode
Strict mode requires you to use let and const when declaring variables. You cannot declare a new variable by just setting a variable's value which would create a global even within a function. Strict mode throws an error if you do not use let or const which will help you catch accidental global creation.
4. What are side effects, and what is a pure function?
Side effects are when functions can modify things that are supposed to be beyond their scope. Side effects cause problems when global variable gets mutated by different functions unintentionally and causes functions to become indeterminate and unpredictable. This could be caused by a function not using let or const and accidently declaring a global variable name which is also used elsewhere in the program. A pure function will always return the same output when given a particular input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment