-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01. Difference between var let const .js
More file actions
157 lines (117 loc) · 3.94 KB
/
01. Difference between var let const .js
File metadata and controls
157 lines (117 loc) · 3.94 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
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
154
155
156
157
// Difference between var, let, and const in JavaScript :
// 1. Scope
// - var: function-scoped or globally scoped
// - let: block-scoped
// - const: block-scoped
// 2. Hoisting
// - var: hoisted and initialized with undefined
// - let: hoisted but not initialized
// - const: hoisted but not initialized
// 3. Reassignment
// - var: can be reassigned
// - let: can be reassigned
// - const: cannot be reassigned (but can mutate objects/arrays)
// 4. Redeclaration
// - var: can be redeclared
// - let: cannot be redeclared in the same scope
// - const: cannot be redeclared in the same scope
// 5. Initialization
// - var: can be declared without initialization
// - let: must be declared before use, cannot be initialized without declaration
// - const: must be initialized at the time of declaration
// *. Also var can add itself to the global object (window in browsers), while let and const do not.
// ----------------------------------------------------------
// using var
var x = 10;
if (true) {
var x = 20; // update x
}
console.log(x); // 20, because var is function-scoped or globally scoped
// using let
let y = 10;
if (true) {
let y = 20; // new y in this block
}
console.log(y); // 10, because let is block-scoped
// using const
const z = 10;
if (true) {
const z = 20; // new z in this block
}
console.log(z); // 10, because const is block-scoped
// ----------------------------------------------------------
// Example of hoisting
console.log(a); // undefined, because var is hoisted and initialized with undefined
var a = 5;
/*
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 10; // let is hoisted but not initialized
console.log(c); // ReferenceError: Cannot access 'c' before initialization
const c = 15; // const is hoisted but not initialized
*/
// ----------------------------------------------------------
// Example of reassignment
var d = 11;
d = 13; // can be reassigned
console.log(d); // 13
let e = 11;
e = 13; // can be reassigned
console.log(e); // 13
const f = 11;
// f = 13; // TypeError: Assignment to constant variable.
// ----------------------------------------------------------
// Example of redeclaration
var g = 17;
var g = 19; // can be redeclared
console.log(g); // 19
// let h = 17;
// let h = 19; // SyntaxError: Identifier 'h' has already been declared
// const i = 17;
// const i = 19; // SyntaxError: Identifier 'i' has already been declared
// ----------------------------------------------------------
/*
JS mainly have two versions, that is ES5 and ES6.
ES5 is the old version of JavaScript, which uses var for variable declaration.
ES6 is the new version of JavaScript, which uses let and const for variable declaration.
*/
// Example of var, let, and const in a function scope :
function example() {
var m = 1;
let n = 2;
const o = 3;
if (true) {
var m = 4; // redeclaring m
let n = 5; // new n
const o = 6; // new o
console.log(m); // 4
console.log(n); // 5
console.log(o); // 6
}
console.log(m); // 4
console.log(n); // 2
console.log(o); // 3
}
example();
// In this example, m is redeclared using var, so it updates the value to 4.
// n and o are block-scoped, so they remain unchanged outside the if block.
function abcd(){
for(var i = 1; i < 12; i++){
console.log(i); // prints 1 to 11
}
console.log(i); // prints 12, because var is function-scoped or globally scoped
}
abcd(); // output: 1 to 11 and then 12
function abcd2(){
for(let i = 1; i < 12; i++){
console.log(i); // prints 1 to 11
}
//console.log(i); // ReferenceError: i is not defined
}
abcd2(); // output: 1 to 11 and then ReferenceError
function abcd3(){
for(const i = 1; i < 12; i++){
console.log(i); // prints 1 to 11
}
//console.log(i); // TypeError: Assignment to constant variable.
}
// abcd3(); // output: 1 to 11 and then TypeError