Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. What is scope? Your explanation should include the idea of global vs. local scope.
  2.  
  3. Scope means the lifetime of a variable.
  4. There are two scopes in javascript: global and local scope.
  5. Global scope means that the variable is available throught the program. Infact, if there are different files then we can even acces that variable across the files.
  6. Local scope is function scope. Whenever a variable is available inside a function and it doesn't exist outside the function is said to be in local scope.
  7. Example:
  8. //global variable
  9. var global_var="test";
  10. function _testfunc(){
  11. var local_var="first";//local variable
  12. console.log(local_var);
  13. }
  14. console.log(global_var);
  15.  
  16. ****************************************************************************************************************************************
  17. Why are global variables avoided?
  18.  
  19. Global variables are avoided as they can be overwritten by some other programmer. The concept of encapsulation of data is voided if we use global variable.
  20. User can see unwanted errors as the global variables are accesible to anyone across the files so due to mishandling of code or handling of code between multiple users can cause it.
  21.  
  22. ******************************************************************************************************************************************
  23. Explain JavaScript's strict mode.What are side effects, and what is a pure function?
  24.  
  25. Strict mode defines that we cannot use undeclared variables. The main idea to "use strict" is to tell that the entire code should be used in strict mode.
  26. This should be added in the beginning of a script.It makes writing javascript more secure.
  27. For eg:-if accidentally anyone mistype a variable name then normal javscript creates a new global variable. In strict mode, it will throw an error.
  28. -Using an object without being declared is not allowed.
  29.  
  30. Pure functions:are the simplest resuable building blocks of code in a program. A pure function is a function which take some input give some output, produces no side effect and does not relie son external state.
  31. Pure functions are completely independent of the outside state.
  32.  
  33. *********************************************************************************************************************************************
  34. Explain variable hoisting in JavaScript.
  35.  
  36. Variable hoisting is a concept which means moving variable declarations to top. That means a variable can be used before it is declared.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement