-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-Destructuring.js
69 lines (49 loc) · 1.36 KB
/
4-Destructuring.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* ------------------------------------------- */
// 1. array destructuring
const numbers = [42, 65];
// const x = numbers[0];
// const y = numbers[1];
// console.log(x,y);
// const [x, y] = [42, 65]
// console.log(x,y);
const [x, y] = numbers;
// console.log(numbers);
function boxify(num1, num2) {
const nums = [num1, num2];
return nums;
}
// console.log(boxify(90, 34));
// const [first, second] = [90, 34]
const [first, second] = boxify(90, 34);
console.log(boxify(90, 34));
const student = {
name: 'Saki Khan',
age: 32,
movies: ['king khan', 'Dhaka Mast']
}
const [firstMovie, secondMovie] = student.movies;
/* ------------------------------------------- */
// 2. object destructuring
// const { name, age } = { name: 'alu', age: 24 };
const { name, age } = { id: 12, name: 'alu', salary: 34000, age: 24 };
const employee = {
ide: 'VS Code',
designation: 'developer',
machine: 'mac',
languages: ['html', 'css', 'js'],
specification: {
height: 66,
weight: 67,
address: 'kumarkhali',
drink: 'water',
watch: {
color: 'black',
price: 500,
brand: 'garmin'
}
}
}
const { machine, ide } = employee;
const { weight, address } = employee.specification;
// using Optional Chaining ( ?. ) -----!!!
const { brand } = employee?.specification?.watch;