Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. What is scope? Your explanation should include the idea of global vs. block scope.
- Scope is the range at which the entire code can access your variables. Global scope means that a variabe can be accessed
- and referenced throughout the entirety of the code. Block scope means that a variable can only be accessed in certain
- "blocks" of your code - i.e. inside a function. If it shares the same name as a variable outside the function (global),
- it will have a different value inside the function.
- 2. Why are global variables avoided?
- Global variables are avoided mostly because they create bugs and unintended side effects throughout the code, that may
- become very difficult to find and fix later on. It can alter varaiable values outside of functions, and change how the
- values are read throughout the code. It makes code become "indeterminate", which means that functions may not always
- return the same value when provided with the same inputs.
- 3. Explain JavaScript's strict mode
- JavaScript's Strict Mode will return an error message whenever a variable is created without using "let" or "const".
- This is important, because variables created without these parameters may return mutated values throughout the code.
- Using Strict Mode is considered best practice when writing in JavaScript, unless there is a specific reason not to do so.
- 4. What are side effects, and what is a pure function?
- Side effects in code refers to when a function reaches outside its own boundaries and starts effecting the rest of the
- code on a "global" scale, which can start to alter the values of other variables, potentially causing bugs to appear.
- This will cause code to become indeterminate, not always returning the same value, again leading to the creation of
- bugs. A Pure Function is the opposite of this - it is when the function is both determinate (always returning the same
- value), and causes no unintended side effects (the function follows block scope) and, like strict mode, it is
- considered best practice to always write pure functions.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement