Skip to content

Commit 62c20f8

Browse files
committed
shorthand coding technique 🚀🚀
1 parent c88ee55 commit 62c20f8

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Longhand:
2+
3+
function fooFunc(bar) {
4+
if (bar === undefined) {
5+
throw new Error('Missing parameter!');
6+
}
7+
return bar;
8+
}
9+
10+
// Shorthand:
11+
12+
mandatory = () => {
13+
throw new Error('Missing parameter!');
14+
};
15+
16+
fooFunc = (bar = mandatory()) => {
17+
return bar;
18+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Longhand
2+
3+
// joining arrays
4+
const odd = [1, 5, 9];
5+
const nums = [2, 4, 6].concat(odd);
6+
7+
// cloning arrays
8+
const arr = [1, 2, 3, 4, 5, 6];
9+
const arr2 = arr.slice();
10+
11+
// Shorthand:
12+
13+
// joining arrays
14+
const odd = [1, 5, 9];
15+
const nums = [2, 4, 6, ...odd];
16+
console.log(nums); // [ 2, 4, 6, 1, 3, 5 ]
17+
18+
// cloning arrays
19+
const arr = [1, 2, 3, 4, 5, 6];
20+
const arr2 = [...arr];
21+
22+
// cloning arrays 1
23+
const odd = [1, 5, 9];
24+
const nums = [2, ...odd, 4, 6, 11, 17];
25+
26+
// combine the spread operator with ES6 destructuring notation:
27+
const { a, b, ...z } = { a: 1, b: 2, c: 5, d: 6 };
28+
console.log(a); // 1
29+
console.log(b); // 2
30+
console.log(z); // { c: 5, d: 6 }

0 commit comments

Comments
 (0)