|
| 1 | + ### 1. Create a Variable using var Keyword and log it to the console |
| 2 | + - Variables declared with var can be redeclared within the same scope without causing an error. |
| 3 | + |
| 4 | + ```JS copy |
| 5 | + var num = 10; |
| 6 | + console.log(num); // 10 |
| 7 | + ``` |
| 8 | + |
| 9 | + **Notes** |
| 10 | + |
| 11 | +- var variables have function scope, meaning they are only accessible within the function they are declared in, or globally if declared outside any function. |
| 12 | +- Declarations with var are hoisted to the top of their scope, but not the initializations. |
| 13 | + |
| 14 | +**References** |
| 15 | + |
| 16 | +- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var |
| 17 | +- https://www.geeksforgeeks.org/javascript-var/ |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +### 2. Create a Variable using let Keyword and log it to the console |
| 22 | + |
| 23 | +- Variables declared with let cannot be redeclared within the same scope, which helps prevent accidental redeclarations and errors. |
| 24 | + |
| 25 | +```JS copy |
| 26 | + let Str = "Hello World"; |
| 27 | + console.log(Str); // "Hello World" |
| 28 | +``` |
| 29 | + |
| 30 | +**Notes** |
| 31 | + |
| 32 | +- let variables are hoisted but not initialized, leading to a "temporal dead zone" where the variable cannot be accessed until the declaration is encountered. |
| 33 | +- let variables have block scope, meaning they are only accessible within the block they are declared in (e.g., inside a set of curly braces {}). |
| 34 | +- let is a keyword introduced in ES6 for declaring variables in JavaScript |
| 35 | + |
| 36 | +**References** |
| 37 | + |
| 38 | +- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let |
| 39 | +- https://www.w3schools.com/js/js_let.asp |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +### 3. Create a constant using const Keyword and log it to the console |
| 44 | + |
| 45 | +- Variables declared with const cannot be redeclared within the same scope, helping to prevent accidental redeclarations and potential bugs. |
| 46 | + |
| 47 | +```JS copy |
| 48 | + const bool = true; |
| 49 | + console.log(bool); // True |
| 50 | +``` |
| 51 | + |
| 52 | +**Notes** |
| 53 | + |
| 54 | +- Variables declared with const cannot be reassigned after their initial assignment. However, if the variable is an object or array, its contents can still be modified. |
| 55 | +- const variables have block scope, meaning they are only accessible within the block they are declared in (e.g., inside a set of curly braces {}). |
| 56 | + |
| 57 | +**References** |
| 58 | + |
| 59 | +- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const |
| 60 | +- https://www.w3schools.com/js/js_const.asp |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +### 4. Make variables with different data types and print their type to the console using typeof method |
| 65 | + |
| 66 | +- The typeof operator is used to determine the type of a given variable or expression, returning a string indicating the type. |
| 67 | + |
| 68 | +```JS copy |
| 69 | + let number = 10; |
| 70 | + let string = "hello World"; |
| 71 | + let boolean = true; |
| 72 | + let object = { |
| 73 | + name: "Chai", |
| 74 | + key: "code" |
| 75 | + }; |
| 76 | + let array = ['Fruits', 'Chai', 'Vegetable']; |
| 77 | + |
| 78 | + console.log(typeof number); // number |
| 79 | + console.log(typeof string); // string |
| 80 | + console.log(typeof boolean); // boolean |
| 81 | + console.log(typeof object); // object |
| 82 | + console.log(typeof array); // object (arrays are of type object in JavaScript) |
| 83 | +``` |
| 84 | + |
| 85 | +**Notes** |
| 86 | + |
| 87 | +- It returns common type strings such as 'number', 'string', 'boolean', 'object', 'function', and 'undefined'. |
| 88 | +- Both arrays and objects return 'object', so typeof does not differentiate between them directly. |
| 89 | + |
| 90 | +**References** |
| 91 | + |
| 92 | +- https://www.w3schools.com/js/js_typeof.asp |
| 93 | +- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +### 5. Declare a variable using let Keyword and assign it a value and reassign its value |
| 98 | + |
| 99 | +- Variables declared with let can be reassigned new values after their initial declaration. |
| 100 | + |
| 101 | +```JS copy |
| 102 | + let a = "Hello World"; |
| 103 | + console.log(a); // "Hello World" |
| 104 | + a = "Hello World 2"; |
| 105 | + console.log(a); // "Hello World 2" |
| 106 | +``` |
| 107 | + |
| 108 | +**Notes** |
| 109 | + |
| 110 | +- Reassignment is subject to the variable's block scope; it can only be reassigned within the same block. |
| 111 | +- While reassignment is allowed, let variables cannot be redeclared in the same scope. |
| 112 | + |
| 113 | +**References** |
| 114 | + |
| 115 | +- https://medium.com/@jsutcliffe1991/reassignment-redeclaration-of-javascript-variables-var-let-const-3135dc02e769 |
| 116 | +- https://www.w3schools.com/js/js_let.asp |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +### 6: Make a constant using const Keyword and try to reassign the value and Identify the error |
| 121 | + |
| 122 | +- Variables declared with const cannot be reassigned a new value after their initial assignment. |
| 123 | + |
| 124 | +```JS copy |
| 125 | + const a = "js Concepts"; |
| 126 | + console.log(a); // "js Concepts" |
| 127 | + a = "JS Concepts & Challenges"; // This line will cause an error |
| 128 | +``` |
| 129 | + |
| 130 | +**Notes** |
| 131 | + |
| 132 | +- The const keyword provides a constant reference to a value, but for objects and arrays, the contents (properties/elements) can still be modified. |
| 133 | +- Like let, const variables have block scope, meaning they are only accessible within the block they are declared in. |
| 134 | + |
| 135 | +**References** |
| 136 | + |
| 137 | +- https://www.w3schools.com/js/js_const.asp |
| 138 | +- https://www.geeksforgeeks.org/javascript-const/ |
| 139 | +- https://dev.to/shearytan/here-is-how-i-change-the-value-of-const-keyword-in-javascript-2gkd |
0 commit comments