Skip to content

Instantly share code, notes, and snippets.

@DikshaSach
Created January 9, 2018 05:58
Show Gist options
  • Save DikshaSach/f916a66ca742244f76761b9a1e6d73dc to your computer and use it in GitHub Desktop.
Save DikshaSach/f916a66ca742244f76761b9a1e6d73dc to your computer and use it in GitHub Desktop.
Scopes are essential in javascript. There are two types of scopes global and local. A global scope is a variable that is set outside a function and the value of this variable can be manipulated throughout the program. A local scope is a variable that is set inside of a function which can only be used in that function and no where else. A local variable however can have the same name as a global variable set outside the function but it is a seperate variable and has no effect on the global varaiable. Variables with global scopes are avoidable because they can be read and manipulated which can lead to the variable changing. This can lead to code errors because if one function manipulated the variable and modified it another function might need the original variable in it but since it has been changed globally the new function will be getting the wrong data.
In normal javascript mistyping a variable name can create a new global variable however in strict mode mistyping will lead to an error and it would be impossible to create a new global variable. Using a variable/object without declaring it is not allowed as well in strict mode. A pure function is a function in which given the same input it will always return the same output and will produce no side effects. Functions with side effects do something other than just returninga value. Side effects are essentially an event that occurs when a function manipulates value out of its scope that was being used globally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment