-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDestructuring.js
58 lines (42 loc) · 988 Bytes
/
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
// destructuring
const user = {
id: 45,
name: 'Shakib',
age: 34,
education: {
degree: "Bachelor"
}
}
// object theke name take ber kore ene arekta variable e assign korte chai jeno onno jaygay use kote pari
// var name = user['name']
const { name, education: {degree: myDegree} } = user // destructuring
console.log(myDegree)
// another uses
console.log('===== Another uses =====')
const user2 = {
id: 45,
name: 'Shakib',
age: 34
}
const { name: title, education: { degree } = {} } = user2
console.log(degree)
// array destructuring
console.log('===== Array destructuring =====')
var numbers = [1, 2, [3, 100, 500], 4, 6]
// var [a, b] = numbers
// get 2 and 6
// var [, a, , , b] = numbers
// get 100 and 500
var [, , [, a, b]] = numbers
console.log(a, b)
// value swapping
console.log('===== value swapping =====')
var p = 1
var q = 2
// the old way
// var temp = a
// a = b
// b = temp
//new way
[q, p] = [p, q]
console.log(p, q)