Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. // Hoisting
  2. console.log(`var before decleration : ${v}`);
  3. console.log(`let before decleration : ${l}`);
  4. console.log(`const before decleration : ${c}`);
  5.  
  6.  
  7. var v = 'VAR'
  8. let l = 'LET'
  9. const c = 'CONST'
  10.  
  11. // SCOPING
  12. (() => {
  13. if (true) {
  14. var v = 'VAR';
  15. let l = 'LET';
  16. const c = 'CONST';
  17.  
  18. console.log(`var in block: ${typeof v !== 'undefined'? v:typeof v}`);
  19. console.log(`let in block: ${typeof l !== 'undefined'? l:typeof l}`);
  20. console.log(`const in block: ${typeof c !== 'undefined'? c:typeof c}`);
  21. }
  22. try {
  23. console.log(`var in function: ${typeof v !== 'undefined' ? v:typeof v}`);
  24. console.log(`let in function: ${typeof l !== 'undefined' ? l:typeof l}`);
  25. console.log(`const in function: ${typeof c !== 'undefined' ? c:typeof c}`);
  26. } catch (e) {
  27. console.log(`${e.name} - ${e.message} in function scope`);
  28. }
  29. })();
  30. try {
  31. console.log(`var out of function: ${typeof v !== 'undefined'? v:typeof v}`);
  32. console.log(`let out of function: ${typeof l !== 'undefined'? l:typeof l}`);
  33. console.log(`const out of function: ${typeof c !== 'undefined'? c:typeof c}`);
  34. } catch (e) {
  35. console.log(`${e.name} - ${e.message} out of function scope`);
  36. }
  37.  
  38. // Redeclaration
  39. (() => {
  40. if (true) {
  41. var v = 'VAR';
  42. let l = 'LET';
  43. const c = 'CONST';
  44.  
  45. const v = 'VAR_re-declared';
  46. const l = 'LET_re-declared';
  47. const c = 'CONST_re-declared';
  48.  
  49. try {
  50. console.log(`var in function: ${typeof v !== 'undefined' ? v:typeof v}`);
  51. console.log(`let in function: ${typeof l !== 'undefined' ? l:typeof l}`);
  52. console.log(`const in function: ${typeof c !== 'undefined' ? c:typeof c}`);
  53. } catch (e) {
  54. console.log(`${e.name} - ${e.message} in function scope`);
  55. }
  56. }
  57. })();
  58.  
  59. // Reassignment
  60. (() => {
  61. if (true) {
  62. var v = 'VAR';
  63. let l = 'LET';
  64. const c = 'CONST';
  65.  
  66. try {
  67.  
  68. v = 'VAR_UPDATED';
  69. l = 'LET_UPDATED';
  70. c = 'CONST_UPDATED'
  71.  
  72. } catch (e) {
  73. console.log(`${e.name} - ${e.message}`);
  74. }
  75. console.log(`var update: ${v}`);
  76. console.log(`let update: ${l}`);
  77. console.log(`const update: ${c}`);
  78. }
  79. try {
  80.  
  81. var v2;
  82. v2 = 10;
  83. let l2;
  84. l2 = 20;
  85. // You must specify its value in the same statement in which its declared
  86. const c2 = 30;
  87.  
  88. console.log(`var 2: ${++v2}`);
  89. console.log(`let 2: ${++l2}`);
  90. console.log(`const 2: ${++c2}`);
  91.  
  92. } catch (e) {
  93. console.log(`${e.name} - ${e.message}`);
  94. }
  95.  
  96.  
  97. // Const with Arrays
  98.  
  99. const name = [];
  100. name.push('pikachu');
  101. name.push('milotic');
  102. console.log(name)
  103.  
  104. const pokemon = {
  105. name: 'Pikachu'
  106. };
  107. pokemon.name = 'Milotic' // this will work ! person variable is not completely reassigned, but mutated
  108. console.log(pokemon.name) // "Milotic"
  109. pokemon = "Kingler" // raises an error, because reassignment is not allowed with const declared variables
  110.  
  111. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement