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 is the scale of what a particular variable can interact with. For instance if a variable is delcared outside of a function the scope would be global. Any future functions that call that particular variable outside the function. Local scope is a variable declared only in inside the function.
- Why are global variables avoided?
- Global variables can cause indeterminate functions, which mean that given the same inputs a function can produce multiple outputs.
- Explain JavaScript's strict mode
- Strict mode assumes all variables decleared without let or const is an error.
- What are side effects, and what is a pure function?
- Side effects are the unintented mutations of varibles. If a global variable is decleared and a function mutates the variable it could cause pure functions to become indeterminate.
- Example below;
- // tip calculator
- const prices = 10;
- const customers = 3;
- // to make this simple set price and customers
- let tip = 0.2;
- // 20% tip as a baseline
- let tax = 0.0175;
- let bill = prices * customers + tax;
- function getTip() {
- bill += tip * bill;
- return bill;
- }
- function receiptPrint() {
- console.log(' Your Bill is = $'+ bill + ' Tip _____' + ' Total ______');
- }
- getTip();
- receiptPrint();
- /* the receipt would print the base bill with the tip included since the getTip function variable is global in scope. */
Add Comment
Please, Sign In to add comment