-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvar-let-const.txt
More file actions
84 lines (61 loc) · 1.09 KB
/
var-let-const.txt
File metadata and controls
84 lines (61 loc) · 1.09 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
VAR-CONST-LET KEYWORDS
1.Var: Can be redeclared & Updated
Ex:
var first="hey"
var first="hello"
console.log(first);
output:hello
2.Let: Can't be redeclared but can be Updated
Ex:
let first="hey"
let first="hello"
console.log(first);
output: error
Ex:
let first="hey"
first="hello"
console.log(first);
output: hello
3.const: can't be redeclared & updated also it must be definitely initialised with some value.
Ex:
const first="hello"
const first="hey"
console.log(first);
output: error
var have Global scope & Function scope
let,const has block scope
Ex:
for(var i=0;i<5;i++)
{
console.log(i+" ");
}
console.log("outside loop value is:"+i);
Output:
0
1
2
3
4
outside loop value is:5
Ex:
for(let i=0;i<5;i++)
{
console.log(i+" ");
}
console.log("outside loop value is:"+i);
Output:
0
1
2
3
4
outside loop value is:error//cant be accessed outside of the loop
Ex:
for(const i=0;i<5;i++)
{
console.log(i+" ");
}
console.log("outside loop value is:"+i);
Output:
0
outside loop value is:Type Error//when i changes from 0 to 1 this error appears coz its a constant it cant be changed or updated