Skip to content

Instantly share code, notes, and snippets.

@rtanubra
Created February 17, 2019 15:03
Show Gist options
  • Save rtanubra/8b915ea2f717cb8b6497675d3737c007 to your computer and use it in GitHub Desktop.
Save rtanubra/8b915ea2f717cb8b6497675d3737c007 to your computer and use it in GitHub Desktop.
Questions to answer:
What is scope? Your explanation should include the idea of global vs. block scope.
Why are global variables avoided?
Explain JavaScript's strict mode
What are side effects, and what is a pure function?
Scope refers to the extent of which parts of your code has access to specific variables/functions declared within the program. Variables declared outside of a function will have global scope. Variables declared within a function will have block scope. Block scope will only be accessible within that block of code (namely, the function). Outside of the function the variable will cease to exist. Global variables will be accessible and modifiable everywhere in your program. Global variables are largely avoided, because functions may inadvertantly modify a global variable, also known as a side effect.
A function is determinate when it produces the same output (given the same input) regardless of when it is run in the program. Global variables tend to make this more challenging. A pure function is a function that is determinate, and has no side effects (except for purely intended side effects). A side effect is when a function affects affects variables outside of its block. An example of an intended side effect would be updating a database (since the database will exist outside of the function). Javascript's strict mode is used to not allow variable declaration outside of let and const. This prevents side effects because utilizing varaiables within a function, the programmer must use let/const to declare new variables first. This removes, ambiguity for JS to identify which variable the program is reffering to.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment