forked from sjimenez77/typescript-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlet-const.ts
64 lines (53 loc) · 1.26 KB
/
let-const.ts
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
//********************* Let **********************
let hello = 'Hello!';
function f(input: boolean) {
let a = 100;
if (input) {
// Still okay to reference 'a'
let b = a + 1;
return b;
}
// Error: 'b' doesn't exist here
return b;
}
for (var i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i);
}, 100 * i);
}
for (let i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i);
}, 100 * i);
}
//********************* Const **********************
const numLivesForCat = 9;
const kitty = {
name: 'Aurora',
numLives: numLivesForCat,
};
// Error
/*kitty = {
name: "Danielle",
numLives: numLivesForCat
};*/
// all "okay"
kitty.name = 'Rory';
kitty.name = 'Kitty';
kitty.name = 'Cat';
kitty.numLives--;
console.log(kitty);
//********************* Destructuring **********************
let input = [1, 2];
let [first, second] = input;
console.log(first); // outputs 1
console.log(second); // outputs 2
//********************* Spread **********************
let foo = [1, 2];
let bar = [3, 4];
let bothPlus = [0, ...foo, ...bar, 5];
// bothPlus = [0, 1, 2, 3, 4, 5];
console.log(bothPlus);
// Spread objects
let defaults = { food: 'spicy', price: '$$', ambiance: 'noisy' };
let search = { ...defaults, food: 'rich', foo: true };