Skip to content

Instantly share code, notes, and snippets.

@travelingtonic
Created October 23, 2018 13:58
Show Gist options
  • Save travelingtonic/4d941df5fadd3c6b072b4a3ce77ca3c4 to your computer and use it in GitHub Desktop.
Save travelingtonic/4d941df5fadd3c6b072b4a3ce77ca3c4 to your computer and use it in GitHub Desktop.
Global Variable Scope
What is scope? Your explanation should include the idea of global vs. local scope.
Variable scope defines how declared variables can be accessed or changed. Global scope variables are defined outside of a function block and can be accessed or changed from anywhere in the same file or even in another file. Block variables are defined within a function block and can only be accessed or changed from within that function block.
Why are global variables avoided?
Global variables can lead to unintended side effects, such as a function could change the value of a global variable unintentionally. Using global variables can cause code to be hard to understand, can make a function indeterminate if it returns different results in some situations vs others, and is hard to troubleshoot.
Explain JavaScript's strict mode.
Strict mode can be defined in a function but preferably is defined at the top of each JavaScript file. Strict mode ensures that all variable defintions must be defined with const or let so that block variables cannot accidentally be created which can override global variable values.
What are side effects, and what is a pure function?
A side effect is when a block variable unintentionally modifies the value of a variable that's outside its function. A pure function is determinate, always returns an expected result, and has no side effects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment