-
Notifications
You must be signed in to change notification settings - Fork 3
/
playground_01.js
203 lines (150 loc) Β· 4.95 KB
/
playground_01.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* eslint-disable */
// !-----------------------
// ! Object property access
// !-----------------------
// ? π [Questions]: Answer the following questions:
// ?
// ? 1. What are the three different ways in JS to access object properties?
// Your answer:
//
//
//
//
// ? 2. When using the `dot property accessor` the property has to be an `identifier`.
// ? Use https://developer.mozilla.org/en-US/docs/Glossary/Identifier to explain what an identifier is.
// Your answer:
//
//
//
// -----------------------------------------------------------------------------
// !---------------
// ! var, let, const
// !---------------
// ! Scope (var)
var newWord = "hello";
const testScope = () => {
newWord = "goodbye";
};
console.log(newWord);
// ! Scope (const)
if (true) {
const word = "hello";
word;
}
word; // undefined
// ! Scope (var)
if (1 == 1) {
var word = "hello";
}
word; // "hello"
// ? π [Question]: Explain the different results for `var` and `const`
// Your answer:
//
//
// ! Re-assignment (const)
const mySum = (one, two, three) => {
return one + two + three;
};
const returnValue = {};
returnValue.sds = mySum(2, 6, 1); // assignment
console.log(returnValue.sds);
// ? π [Question]: Why is this assignment to property of a `const` possible?
// Your answer:
//
//
// -----------------------------------------------------------------------------
// !-------------------------------
// ! Array destructuring and spread
// !-------------------------------
// ! Array spread
// !-------------
const coordinates = [39, 21]; // lat, lng
// Extract second argument only
const [, lng] = coordinates; // equivalent to: `const lng = coordinates[1];`
// ! Spread function arguments
// !--------------------------
const values = [1, 2, 3];
// Elegant solution with spread operator:
const result = mySum(...values);
// Verbose solution without spread operator:
const resultVerbose = mySum(values[0], values[1], values[2]);
console.log(result);
// ! Array destructuring (nested)
// !----------------------
const myArray = [1, [2, 3]];
const [, [, third]] = myArray;
console.log(third);
// ! Array destructuring (fake useState function)
// !----------------------
// Attention: This is a fake `useState` function
// It's not how React.useState function is implemented
const useStateCustom = (initialValue) => {
const stateValue = initialValue;
const setStateValue = (newValue) => {
stateValue.count = newValue.count;
};
return [stateValue, setStateValue];
};
// Array destructuring
const [count, setCount] = useStateCustom({ count: 0 });
setCount({ count: 5 });
console.log(count);
// ! Object destructuring (nested)
// !----------------------
const props = { data: { a: [1, 2, 3], b: [4, 5, 6] } };
const {
data: { a: dataA },
} = props; // nested destructuring + renaming of `a` to `dataA`
console.log(dataA);
// -----------------------------------------------------------------------------
// !---------------------------------------------------------------------
// ! nullish coalescing operator (??) and optional chaining operator (?.)
// !---------------------------------------------------------------------
// let greeting = { say: "hi" };
// console.log(greeting.say?.toUpperCase() ?? "not available"); // "HI"
// console.log(greeting.dontSay?.toUpperCase() ?? "not available"); // "not available"
// ? π [Question]: Explain this interplay of nullish coalescing operator (??) and optional chaining operator (?.)
// Your answer:
//
//
// -----------------------------------------------------------------------------
// !---------------
// ! Type Coercion
// !---------------
// This section is here to demonstrate that the implicit type coercion in JavaScript
// can be very unpredictable. You don't have to (shouldn't) try to understand all of the examples.
// ? π [Task]: Guess the result of each line first and then uncomment the `console.log` statement:
const one = true + false;
// console.log(one);
const two = 1 + 2 + " hello world" + 5 + 3 + 43;
// console.log(two);
const three = 1 + true;
// console.log(three);
const four = "hello" + true;
// console.log(four);
const five = {} + {};
// console.log(five);
const six = {} === {};
// console.log(six);
const seven = [1, 2] == "1,2";
// console.log(seven);
// !--------------------------------
// ! Object.is() comparison operator
// ! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
// !--------------------------------
const eight = Object.is(+0, -0);
// console.log(eight);
const nine = +0 === -0;
// console.log(nine);
const car1 = { wheels: 4 };
const car2 = car1;
const ten = Object.is(car1, car2);
// console.log(ten);
car1.wheels = 5; // mutate original car1 object
const eleven = Object.is(car1, car2);
// console.log(eleven);
const twelve = Object.is(car1, { wheels: 5 });
// console.log(twelve);
console.log(Object.is({ a: "blue" }, { a: "red" }));
// Bonus: Really crazy example:
// https://stackoverflow.com/questions/7202157/why-does-return-the-string-10