-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.html
208 lines (168 loc) · 6.44 KB
/
index.html
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
204
205
206
207
208
<html>
<head>
<title>JavaScript Tips & Trics</title>
</head>
<body>
<h1>All is in the browser console. Open it up.</h1>
<script>
const { log } = console;
// 1. No Concatenation use Template string
{
let name = 'Charlse';
let place = 'India';
let isPrime = bit => {
return (bit === 'P' ? 'Prime' : 'Nom-Prime');
}
let messageConcat = 'Mr. ' + name + ' is from ' + place + '. He is a' + ' ' + isPrime('P') + ' member.'
log(messageConcat);
let messageTemplateStr = `Mr. ${name} is from ${place}. He is a ${isPrime('P')} member.`
log(messageTemplateStr);
}
// 2. isNumber
{
let mynum = 123;
let mynumStr = "123";
let myNumFloat = 123.45;
log(`${mynum} is a number?`, Number.isInteger(mynum));
log(`${mynumStr} is a number?`, Number.isInteger(mynumStr));
log(`${myNumFloat} is a number?`, Number.isInteger(myNumFloat));
}
// 3. Value as Number
function trackChange(event) {
let value = event.target.value;
log(`is ${value} a number?`, Number.isInteger(value));
let valueAsNumber = event.target.valueAsNumber;
log(`is ${value} a number?`, Number.isInteger(valueAsNumber));
}
// 4. Short hand with AND
{
let isPrime = true;
const startWatching = () => {
log('Started Watching!');
}
if (isPrime) {
startWatching();
}
isPrime && startWatching();
}
// 5. Default values with OR
{
let person = {name: 'Jack'};
let age = person.age || 35;
log(`Age of ${person.name} is ${age}`);
}
// 6. Randoms
{
let planets = ['Mercury ', 'Mars', 'Venus', 'Earth', 'Neptune', 'Uranus', 'Saturn', 'Jupiter'];
let randomPlanet = planets[Math.floor(Math.random() * planets.length)];
log('Random Planet', randomPlanet);
let getRandom = (min, max) => {
return Math.round(Math.random() * (max - min) + min);
}
log('Get random', getRandom(0, 10));
}
// 7. Function default params
{
let greetings = (name, message='Hello,') => {
return `${message} ${name}`;
}
log(greetings('Jack'));
log(greetings('Jack', 'Hola!'));
}
// 8. Required Function Params
{
let isRequired = () => {
throw new Error('This is a mandatory parameter.');
}
let greetings = (name=isRequired(), message='Hello,') => {
return `${message} ${name}`;
}
log(greetings('Jack'));
}
// 9. Comma Operator
{
let count = 1;
let ret = (count++, count);
log(count);
}
// 10. Merging multiple objects
{
let emp = {
'id': 'E_01',
'name': 'Jack',
'age': 32,
'addr': 'India'
};
let job = {
'title': 'Software Dev',
'location': 'Paris'
};
// spread operator
let merged = {...emp, ...job};
log('Spread merged', merged);
log('Object assign', Object.assign({}, emp, job));
// for deep merge use _merge of lodash
}
// 11. Destructuring
{
// array
let emojis = ['🔥', '⏲️', '🏆', '🍉'];
let [fire, clock, , watermelon] = emojis;
log(fire, clock, watermelon);
let [f, ...rest] = emojis;
log(f);
log(rest);
// object
let shape = {
name: 'rect',
sides: 4,
height: 300,
width: 500
};
let {name, sides, ...restObj} = shape;
log(name, sides);
log(restObj);
}
// 12. Swap variables
{
let fire = '🔥';
let fruit = '🍉';
[fruit, fire] = [fire, fruit];
log(fire, fruit);
}
// 13. isArray
{
let emojis = ['🔥', '⏲️', '🏆', '🍉'];
log(Array.isArray(emojis));
let obj = {};
log(Array.isArray(obj));
}
// 14. undefined vs null
{
undefined == null // true
undefined === null // false
}
// 15. Get Query Params
{
let project = new URLSearchParams(location.search).get('project');
}
// 16. Tagged Template Literals
{
function introduce(strings, ...values) {
let msg =
`<span style="color:${values[1]}">
Hello ${values[0]}, Have a Nice Day! We know your favorite color is <u>${values[1]}</u>
</span>`;
return msg;
}
const name = 'Joe';
const color = 'green';
const message = introduce`Hello, I'm ${name} and ${color} is my favorite color!`;
log(message);
document.body.innerHTML = message;
}
</script>
<br />
<input type='number' onkeyup="trackChange(event)" />
</body>
</html>