Guest User

Untitled

a guest
Jan 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. // Any varible that is declared with var outside a function block is globally scoped and available for use
  2. // in the whole window. And this can couse issues as a follow one:
  3. var greeter = "hi";
  4. var notCloseFriend = true;
  5.  
  6. if (notCloseFriend) {
  7. var greeter = "say Hello instead";
  8. }
  9.  
  10. console.log(greeter) //"say Hello instead"
  11.  
  12. // We never know if there is identical variable name in the web page because of third-side scripts and our
  13. // local variable might redefine it.
  14.  
  15. // Since let is a block scoped variable, it can be updated but not redeclared:
  16. let greeting = "say Hi";
  17. let greeting = "say Hello instead";//error: Identifier 'greeting' has already been declared
  18. //Unlike var, the 'let' keyword is not initialized as 'undefined' so if you try to use a let variable before
  19. // declaration, you'll get a Reference Error.
  20.  
  21. // The value of a variable declared with const cannot be updated or redeclared:
  22. const greeting = "say Hi";
  23. greeting = "say Hello instead";//error : Assignment to constant variable.
  24.  
  25. const greeting = "say Hi";
  26. const greeting = "say Hello instead";//error : Identifier 'greeting' has already been declared
  27.  
  28. // But! While a const object cannot be updated, the properties of const objects can be updated!
  29.  
  30. // while var variables are initialized with undefined, let and const variables are not initialized and they
  31. // must be initialized during declaration.
  32. // As a general rule it recommends to use let only for loop counters or only if you really need reassignment.
  33. // Everywhere else, use const.
  34.  
  35. // All the var, let and const variables have a curious property: hoisting, where variables and function
  36. // declarations are moved to the top of their scope before code execution.
Add Comment
Please, Sign In to add comment