|
| 1 | + |
| 2 | +# JavaScript Variables |
| 3 | + |
| 4 | + ## Activity 1 |
| 5 | + |
| 6 | + ### Task 1: Create a Variable using var Keyword and log it to the console |
| 7 | + |
| 8 | + **Code:** |
| 9 | + ```jsx live |
| 10 | + var num = 10; |
| 11 | + console.log(num); // 10 |
| 12 | + ``` |
| 13 | + |
| 14 | + **Outcome:** |
| 15 | + - The console will display `10`. |
| 16 | + - The variable `num` is declared using `var` and assigned the value `10`. |
| 17 | + |
| 18 | + ### Task 2: Create a Variable using let Keyword and log it to the console |
| 19 | + |
| 20 | + **Code:** |
| 21 | + ```jsx live |
| 22 | + let Str = "Hello Chai"; |
| 23 | + console.log(Str); // "Hello Chai" |
| 24 | + ``` |
| 25 | + |
| 26 | + **Outcome:** |
| 27 | + - The console will display `Hello Chai`. |
| 28 | + - The variable `Str` is declared using `let` and assigned the value `"Hello Chai"`. |
| 29 | + |
| 30 | + --- |
| 31 | + |
| 32 | + ## Activity 2 |
| 33 | + |
| 34 | + ### Task 3: Create a constant using const Keyword and log it to the console |
| 35 | + |
| 36 | + **Code:** |
| 37 | + ```jsx live |
| 38 | + const bool = true; |
| 39 | + console.log(bool); // True |
| 40 | + ``` |
| 41 | + |
| 42 | + **Outcome:** |
| 43 | + - The console will display `true`. |
| 44 | + - The constant `bool` is declared using `const` and assigned the value `true`. |
| 45 | + |
| 46 | + --- |
| 47 | + |
| 48 | + ## Activity 3 |
| 49 | + |
| 50 | + ### Task 4: Make variables with different data types and print their type to the console |
| 51 | + |
| 52 | + **Code:** |
| 53 | + ```jsx live |
| 54 | + let number = 10; |
| 55 | + let string = "hello World"; |
| 56 | + let boolean = true; |
| 57 | + let object = { |
| 58 | + name: "Chai", |
| 59 | + key: "code" |
| 60 | + }; |
| 61 | + let array = ['Fruits', 'Chai', 'Vegetable']; |
| 62 | + |
| 63 | + console.log(typeof number); // number |
| 64 | + console.log(typeof string); // string |
| 65 | + console.log(typeof boolean); // boolean |
| 66 | + console.log(typeof object); // object |
| 67 | + console.log(typeof array); // object (arrays are of type object in JavaScript) |
| 68 | + ``` |
| 69 | + |
| 70 | + **Outcome:** |
| 71 | + - The console will display: |
| 72 | + ``` |
| 73 | + number |
| 74 | + string |
| 75 | + boolean |
| 76 | + object |
| 77 | + object |
| 78 | + ``` |
| 79 | + - The types of the variables are printed using the `typeof` operator. |
| 80 | + |
| 81 | + --- |
| 82 | + |
| 83 | + ## Activity 4 |
| 84 | + |
| 85 | + ### Task 5: Declare a variable using let Keyword and assign it a value and reassign its value |
| 86 | + |
| 87 | + **Code:** |
| 88 | + ```jsx live |
| 89 | + let chai = "code"; |
| 90 | + console.log(chai); // "code" |
| 91 | + chai = "Hitesh Sir"; |
| 92 | + console.log(chai); // "Hitesh Sir" |
| 93 | + ``` |
| 94 | + |
| 95 | + **Outcome:** |
| 96 | + - The console will display: |
| 97 | + ``` |
| 98 | + code |
| 99 | + Hitesh Sir |
| 100 | + ``` |
| 101 | + - The variable `chai` is declared using `let`, initially assigned the value `"code"`, and then reassigned the value `"Hitesh Sir"`. |
| 102 | + |
| 103 | + --- |
| 104 | + |
| 105 | + ## Activity 5 |
| 106 | + |
| 107 | + ### Task 6: Make a constant using const Keyword and try to reassign the value and Identify the error |
| 108 | + |
| 109 | + **Code:** |
| 110 | + ```jsx live |
| 111 | + const chaiCode = "ChaiCode.com"; |
| 112 | + console.log(chaiCode); // "ChaiCode.com" |
| 113 | + chaiCode = "www.chaicode.com"; // This line will cause an error |
| 114 | + ``` |
| 115 | + |
| 116 | + **Outcome:** |
| 117 | + - The console will display: |
| 118 | + ``` |
| 119 | + ChaiCode.com |
| 120 | + ``` |
| 121 | + - Then, it will throw an error: `TypeError: Assignment to constant variable`. |
| 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 | + |
| 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 | + |
| 202 | + --- |
| 203 | + |
| 204 | + 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. |
| 205 | + |
0 commit comments