Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- What is scope?
- The scope of a variable is region in which that variable is accessible. In Javascript, there are two regions: global and local.
- Variables with a global scope are accessible (and can be mutable, depending on their declaration) by all functions. This can
- even include functions in other files.
- Variables with a local scope, by contrast, are declared within a function, and are only accessible within that function.
- ----
- Why are global variables to be avoided?
- A variable that can be accessed anywhere is prone to unwanted side-effects. Functions, generally, ought to be determinate. That
- is, if the input to a function remains the same, its output should as well. If a function consumes a global variable--one which
- can be used and changed anywhere--its output could be different given the same input on different occassions, if that global
- variable's value changed.
- While this can sometimes be desirable, it can lead to unpredictable and unwanted results. This is especially true as a program
- grows in size and complexity. It is therefore good practice to give a variable the smallest scope needed to do its job.
- ----
- Explain Javascript's strict mode
- Javascript's strict mode raises an exception when a variable is declared without 'let' or 'const'. The purpose is to limit
- unwanted-side effects of manipulating global variables.
- ----
- What are side-effects? What is a pure function?
- Side-effects are changes a function makes outside of itself--changing the value of a global variable, for example. A pure
- function is is determinate (there can be no change in output without a change in input) and has no side-effects.
Add Comment
Please, Sign In to add comment