-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletVarConst.js
61 lines (51 loc) · 1.64 KB
/
letVarConst.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
/**
* Difference between var, let and const
*/
/**
* Variable declarations are processed before the execution of the code.
* The scope of a JavaScript variable declared with var is its current execution context.
* The scope of a JavaScript variable declared outside the function is global.
*/
function varFunction() {
var a = 10;
console.log(a); // 10
// var a = 30; // update globally
// console.log(a); // 30
if (true) {
// overriding the existing value
// a = 20; // update globally
var a = 20; // that the value of variable "a" updated 20 globally
console.log(a); // 20
}
console.log(a); // 20
}
varFunction();
/**
* The let statement declares a local variable in a block scope.
* The let statement allows you to create a variable with the scope limited to the block on which it is used.
*/
function letFunction() {
let a = 10;
console.log(a); // 10
// let a = 20; // Identifier 'a' has already been declared
// console.log(a); //throws type error
if (true) {
// a = 20 // update globally
let a = 20; // scope inside if only
console.log(a); // 20
}
console.log(a); // 10
}
letFunction();
/**
* Declare the const variable in capital letters.
* const statement values can be assigned once and they cannot be reassigned.
* The scope of const statement works similar to let statements.
*/
function constFunction() {
const MY_VARIABLE = 10;
console.log(MY_VARIABLE); // 10
// const MY_VARIABLE = 20; // Identifier 'MY_VARIABLE' has already been declared
// console.log(MY_VARIABLE); //throws type error
}
constFunction();