Skip to content

Instantly share code, notes, and snippets.

@secilreel
Created December 10, 2018 17:25
Show Gist options
  • Save secilreel/92c20522e44dad33c30b3fe8355028d6 to your computer and use it in GitHub Desktop.
Save secilreel/92c20522e44dad33c30b3fe8355028d6 to your computer and use it in GitHub Desktop.
Scope defines where Java Script can find a variable. If the variable is defined within a function, it has a block scope.
Otherwise, if the variable is outside of a function, it has a global scope. Global Scopes should be avoided because it might
change variables in other parts of the code, even outside of the original file. This is an undesirable side effect.Side
effects happen when a function reaches outside of its local scope and up into its parent; and, alters a value that is in there.
A side effect might create an indeterminate code, meaning a code that might not give the same result every time it runs.
To debug such a code would be both hard and frustrating. Instead, it is a good practice to have pure codes, which have no side
effects and are determinate, meaning they give the same result every time it runs. In order to avoid undesired side effects,
and not change variables in the global scope, it is a good practice to write 'strict mode' at the top of Java Script. Strict
mode will only allow changing variables within the file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment