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. local scope.
- Scope determines the accessibility of variables in the program, what variables we have access to from different parts
- of the program. Javascript has two scopes: local scope and global scope.
- Variables declared in global scope are visible and accessible to all programs.
- Variables declared within a function or within block of code (code between curly brackets)
- are local to that function or block of code. Those variables are only visible within their local scope
- (function or block of code), and they are not accessible from the global scope.
- Why are global variables avoided?
- Global variables should be avoided because they can make code unpredictable and produce unexpected results.
- For example, if first function relies on the global variable to properly work, but the second function changes
- that global variables, than the first function would brake.
- When the program has many functions that rely or change global variables, the results such program produces
- could be unreliable and it would be very hard to find the cause of such unfavorable behavior.
- Therefore it is better to use local variables, and use global variables only if it is your intention to change them.
- Explain Javascript’s strict mode.
- Strict mode is a command (“use strict”; ) that is placed at the beginning of a script.
- This mode makes code more secure, by not allowing to use variables without declaring them first.
- Therefore it is not possible to accidentally create global variables in strict mode.
- What are side effects, and what is a pure function?
- A function should work as it is intended to. For example, we have a function that produces the sum of two numbers
- that were passed into it. When we pass in 1 and 1 the result should be 2, no matter how many times we pass
- the same numbers the result should be the same. The pure function always produces same result given same parameters.
- When the same function instead of expected result of 2 will give us every time different number, such function
- is not pure and is not working as it should, because it relies on global variable and at the same time with each
- invocation changing that variable and thus producing side effects.
- Side effects are when a function unintentionally modifies global variable.
Add Comment
Please, Sign In to add comment