Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Prompt:
- 1. What is scope? Your explanation should include the idea of global vs. block scope.
- 2. Why are global variables avoided?
- 3. Explain JavaScript's strict mode
- 4. What are side effects, and what is a pure function?
- Scope has many valuable purposes and is widely used. Scope allows you to set different rules to specify when and how your code
- can access a variable. Two types of scope are Block and Global. Block scope is set between specific functions and only effects
- code inside that function. Global variables are avoided because it allows your code to be bug prone, and harder for multiple
- contributors to work on it. Global variables can cause your code to become indeterminate. Meaning that it can return different
- values. These values can sometimes not be intended, and make it difficult to track bugs. Block scopes allow us to define only
- within the parameters we set. This is an example of a side effect.
- Side effects occur when a function goes outside the local scope and into the parent scope, which can
- cause the code to become indeterminate as mentioned earlier. A function that is determinate (meaning it's a function that always
- returns the same value provided the same inputs) and that also has no side effects is called a pure function. Strict mode acts
- as a security when writing code and avoiding pitfalls. Strict mode will trigger an error code when you do not use
- let or const when defining a variable. Like "<html lang="en">" at the top of a code, we can use 'use strict' ; at the top of our
- Javascript code to enforce it.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement