-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletconstvar.js
93 lines (67 loc) · 2.22 KB
/
letconstvar.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
//Let,const & var
// 1. let : Variable cannot be re-declared but can be updated. A block scope variable.
// 2. const : Variable cannoot be re-declared or updated. A block scope variable.
// 3. var : Variable can be re-declared & updated. A global scope variable.
// let name = "Tony stark";
// let age = 24;
// let totalPrice = 99.99;
// console.log(name);
// console.log(age);
// console.log(totalPrice)
// 1. Example with let
// let age = 45;
// let age = 47;
// console.log(age); it will through an error like : Cannot redeclare block-scoped variable 'age'
// 1.1 Example with let
// let age = 45;
// age = 47;
// console.log(age); // Now it will through an updation of age to 45 to 47
//it will show : 47
// 2. Example with const :
// const age = 17;
// console.log(age); it will print 17 without showing an error
// use case of const : in math equation.
// const age = 17;
// age = 18;
// console.log(age);
// it will show an error because of re-updation : letconstvar.js:33 Uncaught TypeError: Assignment to constant variable
// 3. Example with var
// var age = 24;
// var age = 29;
// var age = 89;
// console.log(age);
// Output will be 89 becuse of its re-declaretions & quietly updation.
//Some of extra learnable things :
// let a; //just declaring a variable without putting any data or only showcasing a variable name declare with name 'a'
// console.log(a);
//Output will be : undefined.
//because of no any kind of data declaration.
//Same of extra learnable things for : const
// const a;
// console.log(a);
//Output will always shows an error : Uncaught SyntaxError: Missing initializer in const declaration
//Because of not declaring any data , Hence const should alwaye be declared!
//block in programming:
// {
// let a = 5;
// console.log(a);
//
// }
//but in block
// {
// let a = 5;
// let a = 12;
// console.log(a);
// }
//In this case it shows an error : Cannot redeclare block-scoped variable 'a'.
//Because of block
//but you can define or re-declare variable via declaring blocks seperately :
// {
// let a = 5;
// console.log(a);
// }
// {
// let a = 12;
// console.log(a);
// }
//Now output shows seperately two both variable with same declaration via no any throwing an