Skip to content

Commit bb33684

Browse files
committed
Intro to JS in functional programming
1 parent 2c5c17d commit bb33684

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// const instead of let and var most of the time
2+
const number = 1;
3+
4+
const arr = [1, 2, 3];
5+
6+
// `const obj = { a: a }` but we have a better way to do it
7+
const a = "a";
8+
const objA = { a };
9+
10+
const b = "b";
11+
const objB = { b };
12+
13+
// composing objects with object spread operator
14+
const ab = { ...objA, ...objB }; // { a: 'a', b: 'b' }
15+
16+
// Destructuring arrays
17+
const [a, b] = ["a", "b"];
18+
a; // 'a'
19+
b; // 'b'
20+
21+
// destructuring objects
22+
const { one } = { one: 1 };
23+
one; // 1
24+
25+
// but also do multiple destructuring
26+
const action = { type: "SUM", data: 1 };
27+
const { type, data } = action;
28+
29+
// we can use this technique in reducers
30+
const reducer = (state = 0, action = {}) => {
31+
const { type, data } = action;
32+
33+
switch (type) {
34+
case "SUM":
35+
return state + data;
36+
default:
37+
return state;
38+
}
39+
};

0 commit comments

Comments
 (0)