-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariables.js
More file actions
117 lines (94 loc) · 5.02 KB
/
Copy pathVariables.js
File metadata and controls
117 lines (94 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 🔸Variables in JavaScript:
// Variables in JavaScript are container used to store and manipulate data.
// In JavaScript, variables are very powerful; they can easily hold any type of data, like numbers, strings, arrays, objects.
// Ways to declare variables:
// 1️⃣ Direct Assignment 2️⃣ Using var 3️⃣ Using let 4️⃣ Using const
//Variable Naming Rules:
//Must start with `A-Z`, `a-z`, `_`, or `$` (❌ no digits first), can contain `0-9`, keywords ❌, case-sensitive 🎯.
// 🔹 Variable Comparison Table
// ___________________________________________________________________________________________________________
// | Keyword | Scope | Reassignment | Redeclaration| Hoisting | Temporal Dead Zone |
// |----------|-------------|---------------|--------------|---------------------------|-------------------- |
// | var | Function | ✅ Yes | ✅ Yes | ✅ Hoisted (`undefined`) | ❌ No |
// | let | Block | ✅ Yes | ❌ No | ✅ Hoisted (but in TDZ) | ✅ Yes |
// | const | Block | ❌ No | ❌ No | ✅ Hoisted (but in TDZ) | ✅ Yes |
// ___________________________________________________________________________________________________________
// 🔹 1. Scope:
// In JavaScript, there are two types of scopes:
// i. Global Scope:
//- Variables declared outside functions and blocks are accessible everywhere.
let globalVar = "I am global";
// ii. Local Scope:
//1️⃣ Function Scope:
//- Variables declared with `var` inside a function are accessible only within that function.
function testScope() {
var functionVar = "I am inside a function";
// console.log(globalVar); // ✅ Accessible
// console.log(functionVar); // ✅ Accessible
}
// console.log(functionVar); // ❌ Error: functionVar is not defined (Function Scope)
//2️⃣ Block Scope: let create a block scope
//- Variables declared with `let` and `const` are only accessible inside the block `{}`.
if (true) {
var blockVar = "I am inside a block";
let blockLet = "I am inside a block";
const blockConst = "I am a constant in a block";
// console.log(blockLet); // ✅ Accessible inside block
// console.log(blockConst); // ✅ Accessible inside block
}
// console.log(blockVar); // ✅ Accessible (var is function-scoped, not block-scoped)
// console.log(blockLet); // ❌ Error: blockLet is not defined (Block Scope)
// console.log(blockConst); // ❌ Error: blockConst is not defined (Block Scope)
//🔹2. Reassignment & Redeclaration
// 1. Using `var`
var a = 10; // ✅ Declaration
a = 20; // ✅ Reassignment allowed
var a = 30; // ✅ Redeclaration allowed
// 2. Using `let`
let b = 40; // ✅ Declaration
b = 50; // ✅ Reassignment allowed
// let b = 60; // ❌ Error: Redeclaration not allowed
// 3. Using `const`
const c = 70; // ✅ Declaration
// c = 80; // ❌ Error: Reassignment not allowed
// const c = 90; // ❌ Error: Redeclaration not allowed
// 4. Special case: `const` with objects (Mutability)
const obj = { name: "John" }; //{ name: "John" }
obj.name = "Doe"; // ✅ Allowed (Object properties can be modified)
// console.log(obj); // { name: "Doe" }
// obj = {}; // ❌ Error: Reassignment of `const` not allowed
const arr = [1, 2, 3];
arr.push(4); // ✅ Allowed (Mutability)
// arr = [5, 6, 7]; // ❌ Error: Reassignment not allowed
//🔹3. Hoisting & Temporal Dead Zone
// 1. Hoisting with `var`
// console.log(varVariable); // ✅ Undefined (Hoisted)
var varVariable = 10;
// 2. Hoisting with `let` (Temporal Dead Zone)
try {
// console.log(letVariable); // ❌ Error: Cannot access 'letVariable' before initialization (TDZ)
} catch (error) {
console.log(error.message);
}
let letVariable = 20;
// 3. Hoisting with `const` (Temporal Dead Zone)
try {
// console.log(constVariable); // ❌ Error: Cannot access 'constVariable' before initialization (TDZ)
} catch (error) {
console.log(error.message);
}
const constVariable = 30;
// 4. Function Hoisting
// hoistedFunction(); // ✅ Works because function declarations are fully hoisted
function hoistedFunction() {
console.log("I am hoisted!");
}
// 5. Function Expression (Not Hoisted)
try {
// nonHoistedFunction(); // ❌ Error: Cannot access 'nonHoistedFunction' before initialization
} catch (error) {
console.log(error.message);
}
const nonHoistedFunction = function () {
console.log("I am not hoisted!");
};