-
Notifications
You must be signed in to change notification settings - Fork 0
/
var-let-comparison.js
53 lines (44 loc) · 1.39 KB
/
var-let-comparison.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
// Global declaration - global var variable stores in window object.
// var location = "hello"
// It will try to redirect to hello
// window.location = "hello"
/*
* var location is similar to window.location
* global var variable stores in window object.
* var declared inside the function stores in stack
* -----------------------------------------------------
* There is chance to corrupts the window object value
* @ let location = "hello"
* let will not corrupts the window object
* -----------------------------------------------------
*/
/*----------------------------------------------------------------------------*/
function useBfDeclaration() {
// Hoisting
// var num ===> js read as: the declared variable will put in top of the code, not values
console.log(num)
var num = 12;
var num;
var num;
}
useBfDeclaration()
function useAfDeclaration() { //---block start
// console.log(num1)
let num1 = 12;
// let num1;
// let num1;
} //--- block end
useAfDeclaration() // Cannot access 'num1' before initialization
/*
* var support hoisting
* let will not support
*/
/*
* var support multiple declaration in a scope due to hoisting
* let will not support multiple declaration in a scope
*/
/*
* var-function scope: scope of a var is function
* let-block scope: scope of a let is block - child block will get the scope
*/
/*----------------------------------------------------------------------------*/