Advertisement
rajuahammad73

02. Javascript Scope Lesson

Mar 14th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*****************************************Higher Order Functions************************************ */
  2.  
  3. // 1.Assigning Functions to Variables
  4.  
  5. // const square = function(x) {
  6. //   return x * x;
  7. // };
  8. // console.log(square(5));
  9.  
  10. // const foo = square;
  11. // console.log(foo(6));
  12.  
  13. // 2.Passing Functions as Parameters
  14.  
  15. // function sayHello() {
  16. //   return "Hello, ";
  17. // }
  18. // function greeting(fn, name) {
  19. //  console.log(fn() + name);
  20. // }
  21.  
  22. // greeting(sayHello, "JavaScript!");
  23.  
  24.  
  25. // 3. Return a function
  26.  
  27. // function sayHello() {
  28. //   var a = "Hello"
  29. //   return function() {
  30. //     console.log(a);
  31. //   };
  32. // }
  33. // console.log(sayHello()());
  34.  
  35. // Global
  36. // Local
  37. // Block
  38. // Lexical
  39.  
  40.  
  41.  
  42. /************************************** Variables ***************************************************** */
  43.  
  44. // Variable - Var
  45.  
  46. // console.log(greeter);
  47. // var greeter = "say hello";
  48. // var greeter = "hello again";
  49. // console.log(greeter);
  50.  
  51. // Weakness of var
  52.  
  53. // var greeter = "hey hi";
  54. // var times = 4;
  55.  
  56. // if (times > 3) {
  57. //     var greeter = "say Hello instead";
  58. // }
  59.  
  60. // console.log(greeter)
  61.  
  62. // Varibale - LET
  63.  
  64. //1. let is block scoped
  65.  
  66. // let greeting = "say Hi";
  67. // let times = 4;
  68.  
  69. // if (times > 3) {
  70. //      let hello = "say Hello instead";
  71. //      console.log(hello);//"say Hello instead"
  72. //  }
  73. // console.log(hello) // hello is not defined
  74.  
  75. //2. let can be updated but not re-declared.
  76.  
  77. // let greeting = "say Hi";
  78. // let greeting = "say Hello instead";
  79. // console.log(greeting)
  80.  
  81. // Varibale - CONST
  82.  
  83. // 1.const declarations are block scoped
  84.  
  85. // 2. const cannot be updated or re-declared
  86.  
  87. // Differences
  88.  
  89. // 1. var declarations are globally scoped or function scoped while let and const are block scoped.
  90.  
  91. // 2. var variables can be updated and re-declared within its scope;
  92. // let variables can be updated but not re-declared;
  93. // const variables can neither be updated nor re-declared.
  94.  
  95. // 3. They are all hoisted to the top of their scope but while varvariables are initialized with undefined, let and const variables are not initialized.
  96.  
  97. // 4. While var and let can be declared without being initialized, const must be initialized during declaration.
  98.  
  99. // var a;
  100. // a = 10;
  101. // let b;
  102. // b = 20;
  103. // const c = 44;
  104. // console.log(b)
  105.  
  106.  
  107. // ************************ Conditions ***********************************/
  108.  
  109. // Ternary Operator
  110.  
  111. // let age = 80;
  112. // let message = age > 18 ? true : false;
  113. // console.log(message)
  114.  
  115. // Ternaray Opearator - Multiple Conditions
  116. //  let age = 80;
  117. // let message;
  118. // if (age < 3) {
  119. //   message = 'Hi, baby!';
  120. // } else if (age < 18) {
  121. //   message = 'Hello!';
  122. // } else if (age < 100) {
  123. //   message = 'Greetings!';
  124. // } else {
  125. //   message = 'What an unusual age!';
  126. // }
  127. // console.log(message)
  128.  
  129. // let message = (age < 3) ? 'Hi, baby!' :
  130. //   (age < 18) ? 'Hello!' :
  131. //   (age < 100) ? 'Greetings!' :
  132. //   'What an unusual age!';
  133.  
  134. //  console.log(message)
  135.  
  136. // undefined, null, 0, "", NaN
  137.  
  138. // Short Circuit Evaluation
  139.  
  140.  
  141. // if (-1 && 0) {
  142. //   console.log("daf");
  143. // }
  144.  
  145. // var a = 11;
  146. // var n = a && 4 && 10 && 6;
  147. // console.log(n)
  148.  
  149.  
  150. // let a = 2 + 2;
  151.  
  152. // switch (a) {
  153. //   case 3:
  154. //     console.log('Too small')
  155. //     break;
  156. //   case 4:
  157. //     console.log('Exactly')
  158. //     break;
  159. //   case 5:
  160. //     console.log('Too large')
  161. //     break;
  162. //   default:
  163. //     console.log("I don't know such values")
  164. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement