|
137 | 137 | - https://www.w3schools.com/js/js_const.asp |
138 | 138 | - https://www.geeksforgeeks.org/javascript-const/ |
139 | 139 | - https://dev.to/shearytan/here-is-how-i-change-the-value-of-const-keyword-in-javascript-2gkd |
| 140 | +--- |
| 141 | +### 7. Demonstrate Hoisting with var |
| 142 | +Show how var is hoisted to the top of its scope. |
| 143 | + |
| 144 | +```javascript |
| 145 | + |
| 146 | +console.log(myVar); // undefined |
| 147 | +var myVar = "Hoisted!"; |
| 148 | +console.log(myVar); // "Hoisted!" |
| 149 | +``` |
| 150 | +**Notes** |
| 151 | + |
| 152 | +- Variable declarations with var are hoisted to the top of their scope, but their initializations are not. |
| 153 | + |
| 154 | +**References** |
| 155 | + - https://developer.mozilla.org/en-US/docs/Glossary/Hoisting |
| 156 | +--- |
| 157 | + |
| 158 | +### 8. Create a Function to Demonstrate Scope of let |
| 159 | +Demonstrate the difference between block scope and function scope using let. |
| 160 | + |
| 161 | +```javascript |
| 162 | + |
| 163 | +function testScope() { |
| 164 | + let x = "I'm inside the function"; |
| 165 | + if (true) { |
| 166 | + let y = "I'm inside the block"; |
| 167 | + console.log(y); // "I'm inside the block" |
| 168 | + } |
| 169 | + // console.log(y); // Uncaught ReferenceError: y is not defined |
| 170 | + console.log(x); // "I'm inside the function" |
| 171 | +} |
| 172 | +testScope(); |
| 173 | +``` |
| 174 | +**Notes** |
| 175 | + |
| 176 | +- This example illustrates how let is scoped to the nearest enclosing block, preventing access outside of it. |
| 177 | + |
| 178 | +**References** |
| 179 | + - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let |
| 180 | +--- |
| 181 | +### 9. Create Variables with Object and Array and Log Their Types |
| 182 | +Show how to create and log an object and an array's type. |
| 183 | + |
| 184 | +```javascript |
| 185 | + |
| 186 | +let user = { |
| 187 | + name: "Alice", |
| 188 | + age: 30 |
| 189 | +}; |
| 190 | +let colors = ["red", "green", "blue"]; |
| 191 | + |
| 192 | +console.log(typeof user); // "object" |
| 193 | +console.log(typeof colors); // "object" (arrays are objects) |
| 194 | +``` |
| 195 | +**Notes** |
| 196 | + |
| 197 | +- Both arrays and objects are identified as "object" type in JavaScript. |
| 198 | + |
| 199 | +**References** |
| 200 | + - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures |
| 201 | +--- |
0 commit comments