-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiving-Default-Value.js
52 lines (40 loc) · 1.04 KB
/
Giving-Default-Value.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
// Giving Default Value
// Object #1
const mhs = {
nama: 'Ken Arok',
umur: 35
}
// Destructuring Object #1 with additional value
const { nama, umur, email = 'kenArok@default.com' } = mhs;
// Default Value
console.log(nama); // output: Ken Arok
console.log(umur); // output: 35
// Additional value
console.log(email); // output: kenArok@default.com
// Object #1 does not change after destructuring
console.log(mhs);
// Output:
// {
// nama: "Ken Arok",
// umur: 35
// }
// Object #2
const mhsa = {
nama_a: 'Pari Kesit',
umur_a: 27,
email_a: 'pariKesit@contoh.com'
}
// Destructuring Object #2
const { nama_a, umur_a, email_a = 'pariKesit@default.com'} = mhsa; // trying to change default value of email_a
// Default Value
console.log(nama_a); // Pari Kesit
console.log(umur_a); // 27
console.log(email_a); // pariKesit@contoh.com (email_a is keeping its default value)
// Object #2 does not change after destructuring
console.log(mhsa);
// Output:
// {
// email_a: "pariKesit@contoh.com",
// nama_a: "Pari Kesit",
// umur_a: 27
// }