|
121 | 121 | - Then, it will throw an error: `TypeError: Assignment to constant variable`. |
122 | 122 | - The constant `chaiCode` is declared using `const` and assigned the value `"ChaiCode.com"`. Attempting to reassign it results in an error since constants cannot be reassigned. |
123 | 123 |
|
124 | | - --- |
125 | | - |
126 | | - ## Feature Request 1 |
127 | | - |
128 | | - **Code:** |
129 | | - ```jsx live |
130 | | - let number1 = 10; |
131 | | - let string1 = "hello World"; |
132 | | - let boolean1 = true; |
133 | | - let object1 = { |
134 | | - name: "Chai", |
135 | | - key: "code" |
136 | | - }; |
137 | | - let array1 = ['Fruits', 'Chai', 'Vegetable']; |
138 | | - |
139 | | - console.log(`${number1} is a ${typeof number1}`); |
140 | | - console.log(`${string1} is a ${typeof string1}`); |
141 | | - console.log(`${boolean1} is a ${typeof boolean1}`); |
142 | | - console.log(`${object1} is a ${typeof object1}`); |
143 | | - console.log(`${array1} is a ${typeof array1}`); |
144 | | - ``` |
145 | | - |
146 | | - **Outcome:** |
147 | | - - The console will display: |
148 | | - ``` |
149 | | - 10 is a number |
150 | | - hello World is a string |
151 | | - true is a boolean |
152 | | - [object Object] is a object |
153 | | - Fruits,Chai,Vegetable is a object |
154 | | - ``` |
155 | | - - This shows the type of each variable using template literals and `typeof`. |
156 | | - |
157 | | - --- |
158 | | - |
159 | | - ## Feature Request 2 |
160 | | - |
161 | | - **Code:** |
162 | | - ```jsx live |
163 | | - let hello = "world"; |
164 | | - console.log(hello); |
165 | | - hello = "Hello Chai"; |
166 | | - |
167 | | - const HiteshSir = 'Chai Aur Code'; |
168 | | - console.log(HiteshSir); |
169 | | - HiteshSir = 'www.chaicode.com'; // This line will cause an error |
170 | | - ``` |
171 | | - |
172 | | - **Outcome:** |
173 | | - - The console will display: |
174 | | - ``` |
175 | | - world |
176 | | - Hello Chai |
177 | | - Chai Aur Code |
178 | | - ``` |
179 | | - - Then, it will throw an error: `TypeError: Assignment to constant variable`. |
180 | | - - The variable `hello` is declared using `let`, initially assigned the value `"world"`, and then reassigned the value `"Hello Chai"`. |
181 | | - - The constant `HiteshSir` is declared using `const` and assigned the value `'Chai Aur Code'`. Attempting to reassign it results in an error since constants cannot be reassigned. |
182 | | - |
183 | | - --- |
184 | | - |
185 | | - **Summary of Key Learnings:** |
186 | | - |
187 | | - 1. **Variable Declarations:** |
188 | | - - `var` declares variables with function scope or globally if declared outside a function. |
189 | | - - `let` declares variables with block scope, allowing reassignment. |
190 | | - - `const` declares variables with block scope, but they cannot be reassigned after the initial assignment. |
191 | | - |
192 | | - 2. **Type Checking:** |
193 | | - - The `typeof` operator is used to determine the type of a variable. |
194 | | - |
195 | | - 3. **Reassignment:** |
196 | | - - Variables declared with `let` can be reassigned. |
197 | | - - Constants declared with `const` cannot be reassigned, and attempting to do so results in an error. |
198 | | - |
199 | | - 4. **Template Literals:** |
200 | | - - Template literals (using backticks) allow embedding expressions within strings using `${expression}`. |
201 | | - |
| 124 | + |
202 | 125 | --- |
203 | 126 |
|
204 | 127 | This CHallenge provides a comprehensive overview of JavaScript variables, covering variable declarations (`var`, `let`, `const`), type checking, reassignment rules, and usage examples. Each section includes code snippets, expected outcomes, and summaries of key learnings. |
0 commit comments