Guest User

Untitled

a guest
Jan 21st, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. Scope is the context in which variables are defined. In a Javascript file, there are global and local scopes. Global scopes are variables that are defined outside of any function and can be called from anywhere in the document. Local scopes, also known as "block scope", by contrast are variables declared within a function and exist solely inside of that function. Once that function is done running, the variable is cleared. Global scope should generally be avoided due to the mutable nature of it. That is, there arises a greater chance of improper variable use which ends up changing the variable through 'scope chain' - the ability for variables inside of a block scope to reference a variable outside of the function should that variable not exist inside the block.
  2. One way to avoid this problem is to insert Javascript's 'strict mode' at the beginning of the document. Strict mode prevents undeclared variables in block scopes from calling up scope chain by issuing an error. Use of global variables leads to indeterminate code - idea that functions can not be relied upon as a result of different given outcomes. A pure function, then, is the opposite - determinate code which avoids the pitfalls of global side effects.
Add Comment
Please, Sign In to add comment