Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- What is scope? Your explanation should include the idea of global vs. block scope.
- According to Merriam-Webster, scope is a range of operation. In Javascript,
- scope defines the range of operation of declared variables and more importantly our access to these variables
- dependant upon where in the code they were declared. Variable scope are rules defining from which parts of your code
- you can access a particular variable.
- Global scope refers to the entire Javascript program, application or files.
- Variables declared in the global scope can be accessed from anywhere in your code, inside and outside of functions.
- A variable with block scope means the variable is only accessible from within the function’s block of instructions.
- When the function is invoked, the variable is called and when the function is done executing, the variable disappears.
- Which leads to, why should global variables be avoided?
- Global variables should be avoided because they make unintended side effects in a program more likely.
- This usually happens when a function reaches outside its block scope into the global scope to access and/or alter a value
- that lives there. This almost guarantees that code becomes indeterminate, meaning when given a single set of inputs
- it returns one value one time and a different value another time. We want determinate code that will always return the
- same value given the same inputs. Indeterminate code leads to hard to find bugs and frustration.
- What is Javascript’s strict mode?
- Strict mode makes it easier to write “secure” Javascript. With strict mode enabled, a variable must be declared
- with let or const or an error will be triggered. Also, in JavaScript without strict mode enabled,
- a mistyped variable name creates a new global variable. In strict mode this produces an error and makes it
- impossible to accidentally create a global variable.
- What are side effects, and what is a pure function?
- Side effects are usually unintended and occur when a function reaches out of it’s local block scope into the parent or
- global scope and accesses or alters a value that lives there. This leads to indeterminate code which doesn’t
- consistently return the same values given the same inputs. When code is determinate, consistently returning the same values
- for the same inputs and has no side effects, this is considered pure function. Pure functions are easier to read,
- easier to debug, easier to test and operate consistently.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement