-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseJavascript.js
More file actions
63 lines (54 loc) · 1.38 KB
/
useJavascript.js
File metadata and controls
63 lines (54 loc) · 1.38 KB
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
let a = 5,
b = 10;
[a, b] = [b, a];
console.log(a, b);
let sum = 0;
for (let i = 5; i < 10; i += 1) {
sum += i;
}
console.log(sum);
const sum2 = Array.from(new Array(5), (_, k) => k + 5).reduce(
(acc, cur) => acc + cur,
0
);
console.log(sum2);
//배열 내 같은 요소 제거
const names = ["Lee", "Kim", "Park", "Lee", "Kim"];
const uniqueNamesWithArrayFrom = Array.from(new Set(names));
const uniqueNamesWithSpread = [...new Set(names)];
console.log(uniqueNamesWithArrayFrom);
console.log(uniqueNamesWithSpread);
//Spread 연산자를 이용한 객체 병합
const person = {
name: "Lee Sun-Hyoup",
familyName: "Lee",
givenName: "Sun-Hyoup",
};
const company = {
name: "Cobalt. Inc.",
address: "Seoul",
};
const leeSunHyoup = { ...person, ...company };
console.log(leeSunHyoup);
/// ||
// participantName이 0, undefined, 빈 문자열, null일 경우 'Guest'로 할당
const participantName = 0;
const name = participantName || "Guest";
/// &&
// flag가 true일 경우에만 실행
const flag = true;
function func() {
console.log("true && func()");
}
flag && func();
// 객체 병합에도 이용가능!
const makeCompany = (showAddress) => {
return {
name: "Cobalt. Inc.",
...(showAddress && { address: "Seoul" }),
};
};
console.log(makeCompany(false));
// { name: 'Cobalt. Inc.' }
console.log(makeCompany(true));
// { name: 'Cobalt. Inc.', address: 'Seoul' }