Guest User

Untitled

a guest
Mar 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. What is Scope? Global vs local
  2.  
  3. Scope is what determines the accessibility of a variable in different parts of your code. Scope is important because it helps you to understand how the code works and also is helpful in avoiding bugs. For example, if you try to access a variable that is out of scope it will raise an error. Two main types of scope are referred to a global and local. A variable is said to have local scope when it is declared inside of a function block. What it also means is that the variable is only accessible inside of the function block and trying to use it outside of the function will raise a reference error. In contrast, a global variable is defined outside of a function and means that it can be accessed anywhere and everywhere in the code. It is even possible for a global variable’s scope to extend into separate files.
  4.  
  5. Why should global variables be avoided?
  6.  
  7. There are a few reasons that Global variables should be avoided unless it is required. One reason is that global variables are prone to have unintended side effects in a program resulting in the code becoming indeterminate. What this means is that the same inputs can result in unpredictable outcomes and make the debugging process more difficult.
  8.  
  9. Explain JavaScript’s strict mode
  10.  
  11. When strict mode in JavaScript is activated it means that the code is typed in a more constrained type of JavaScript. It is useful because it will throw an error on problems with the code that regular JavaScript might ignore. For example, in strict mode the key words let and const must be used to declare a variable or an error will be raised. In regular JavaScript it is possible to declare a variable without let or const ( myVar = “hi there”; ) and instead of an error it is made into a variable with global scope. Turning strict mode is done by adding the expression ‘use strict’ at the beginning of a file and is considered a best practice to do so.
  12.  
  13.  
  14.  
  15. What are side effects,and what is a pure function
  16.  
  17. A side effect is when a function reaches outside of its scope into the parent scope and changes a value that lives there. What this means it that the value of a global function can be unintentionally changed if the variable’s value inside of a function is changed. Now if later in your code you want to use the global variable’s initial value it won’t work because it now has the value assigned from the function. A pure function is when it is both determinate, or it will always return the same value with the same inputs, and it has no side effects on your code.
Add Comment
Please, Sign In to add comment