-
Notifications
You must be signed in to change notification settings - Fork 0
/
Let_VS_var_VS_const.js
153 lines (118 loc) · 3.89 KB
/
Let_VS_var_VS_const.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// VAR
function show(){
// var e ; // Hoisting
console.log("E is ", e); // undefined
if(10>2){
var e = 10; // e = 10;
}
console.log(e);
}
show();
E is undefined
10
undefined
function show(){
console.log("E is ", e); // undefined
var e = 20;
console.log("E is ", e); // 20
if(10>2){
var e = 10; // e = 10;
console.log("E is ", e); // 10
}
var e = 30;
console.log(e); //30;
}
show();
E is undefined
E is 20
E is 10
30
// LET
function show(){
console.log("E is ", e); // Error
if(10>2){
let e = 10; // e = 10; // here let has block scope, so can't able to do hoisting outside function
console.log("E is ", e); // 10
}
console.log(e); //Error;
}
show();
VM325:2 Uncaught ReferenceError: e is not defined
at show (<anonymous>:2:27)
at <anonymous>:1:1
show @ VM325:2
(anonymous) @ VM346:1
// ok so now try to acces it in function level.
function show(){
if(10>2){ // here, Hoisting is done inside the if statement but it wasn't initialized.
console.log("E is ", e);
let e = 10; // e = 10;
console.log("E is ", e); // 10
}
console.log(e); //Error;
}
// still this will give Error, bcz let jha par declare hota h hmesha uske niche se hi execution start krta hai.
// anything that is declared in the function scope above the let , will be treated as TDZ (Temporal Dead zone).
// variable declare to hota hai lekin initialize ni hota.. Undefined se bhi initialize ni hota. isliye memory m ni hota
show();
VM356:5 Uncaught ReferenceError: Cannot access 'e' before initialization
at show (<anonymous>:5:30)
at <anonymous>:1:1
show @ VM356:5
(anonymous) @ VM378:1
function show(){
let e = 20;
console.log('E is ', e);
if(10>2){
let e = 10; // e = 10;
console.log("E is ", e); // 10
}
console.log(e); // 20
}
show();
E is 20
E is 10
20
function show(){
let e = 20;
console.log('E is ', e); // 20
if(10>2){
let e = 10; // e = 10;
console.log("E is ", e); // 10
}
let e = 100;
console.log(e); //Error, bcz of redeclaration, mutliple declaration is only allowed in different scope
}
VM469:10 Uncaught SyntaxError: Identifier 'e' has already been declared // re-declaration wasn't allowed.
let rr = 100;
window.rr; // we can't access let on window object because let jha par declare hota h hmesha uske niche se hi execution start krta hai.
undefined // and window is on the top.
// CONST
const MAX = 100;
MAX++;
VM586:1 Uncaught TypeError: Assignment to constant variable. // we can't increament constant values.
at <anonymous>:1:4
(anonymous) @ VM586:1
MAX = 200;
VM607:1 Uncaught TypeError: Assignment to constant variable. // we can't re-declare constant values.
at <anonymous>:1:5
(anonymous) @ VM607:1
const obj = {id:1001, name:'Ram'}; // this is shallow constant, bcz it has reference
obj.id++; // This is possible because in obj reference is stored not the values. and we are only changing values instead of addresses.
1001
obj;
{id: 1002, name: 'Ram'} // no concept of deep constant
obj = {}; // This is not possible because we are trying to change the address, that is constant.
VM734:1 Uncaught TypeError: Assignment to constant variable.
at <anonymous>:1:5
(anonymous) @ VM734:1
// JS doesn't support deep constant.
const arr = [10,20,30];
arr[0] = 1111; // This is possible because in obj reference is stored not the values. and we are only changing values instead of addresses.
1111
arr;
(3) [1111, 20, 30]
arr = [100,200,300]; // This is not possible because we are trying to change the constant address.
VM911:1 Uncaught TypeError: Assignment to constant variable.
at <anonymous>:1:5
(anonymous) @ VM911:1