-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (45 loc) · 1.71 KB
/
index.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
(function(context) {
var globalVar = 'global var';
let globalLet = 'global let';
function demoVsVar() {
console.log(demoVarDeclaredLater);
var demoVarDeclaredLater = 'demo var declared later';
// console.log(demoLetDeclaredLater); Uncaught ReferenceError: demoVarDeclaredLater is not defined
let demoLetDeclaredLater = 'demo var declared later';
for (var i = 0; i < 5; i ++) {
setTimeout(() => console.log(`value of var i: ${i}`), 100);
}
for (let j = 0; j < 5; j ++) {
setTimeout(() => console.log(`value of let i: ${j}`), 100);
}
}
function demo() {
console.log('\n\nLET CONST VAR');
globalVar = 'global var changed in method scope';
console.log(globalVar);
globalLet = 'global let changed in method scope';
console.log(globalLet);
/*
If a global let is defined again in child scope, will cause error
Uncaught ReferenceError: globalLet is not defined
let globalLet = 'global Let';
console.log(globalLet);
*/
var demoVar = 'demo var';
let demoLet = 'global let';
const demoConst = 'I am constant';
// let maintains block scope
// var maintains function scope
// It is a good practice to always declare variables as const
// unless you need to re-assign it with new value later, in such cases use let
console.log(demoVar);
console.log(demoLet);
console.log(demoConst);
// demoConst = 'I am constant reassigned'; Uncaught TypeError: Assignment to constant variable.
demoVsVar();
};
console.log(globalVar);
console.log(globalLet);
// console.log(demoVar); Uncaught ReferenceError: demoVar is not defined
(context || this).demoLibs['let-const-var'] = demo;
})(window);