Skip to content

Instantly share code, notes, and snippets.

@michelleroth
Created November 15, 2016 05:11
Show Gist options
  • Save michelleroth/6e0e9303a110d35929ef5644ce0653c9 to your computer and use it in GitHub Desktop.
Save michelleroth/6e0e9303a110d35929ef5644ce0653c9 to your computer and use it in GitHub Desktop.
JS has two scopes which can be defined as a region of the program; local and global. A variable declared outside of a function becomes part of the global scope, but if it is declared within the function it is encompassed by the local scope. By declaring a variable a part of the global scope, you are making it vulnerable to error and bugs, as it is accessible to from any code within the program you are modifying.
As mentioned above, global variables create more opportunity for errors and problems as it is accessible from anywhere within the program and can cause unforeseen large scale problems if something goes wrong and the wrong functions or variables are affected.
Strict mode is a safe guard, and it is designed to warn you if you are sending something into the global scope. It is written initially as a rule before the syntax is written and is commonly used.
Side effects are accidental issues caused primarily with global scope. Everything that is not a planned return but ends up causing problems for other pieces of code fall under this category. A pure function is deal, it is a clean and simple function where the output is a direct response of what argument is assigned. You know what return you are getting after you run your function.
Variable hoisting in JS is what happens in the background of JS when you assign or declare a variable. When you test that result, the outcomes appears undefined and this is because the program has hoisted the variable or function declaration to the top of the scope, be it global or local. JS has left the assignment of that variable or function in place, so it appears that your variable or function is missing it’s assignment, but this is not the case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment