-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestructuring.js
More file actions
104 lines (59 loc) · 1.88 KB
/
destructuring.js
File metadata and controls
104 lines (59 loc) · 1.88 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// destructuring = extract values from arrays and objects,
// then assign them to variables in a
// convenient way.
// [] = to perform array destructuring
// {} = to perform object destructuring
// 5 examples
// -----------------------example-1
// **SWAP THE VALUE ODF TWO VARIABLE
// let a = 1;
// let b = 2;
// [a, b] = [b, a];
// console.log(a);
// console.log(b);
// --------------------------example -2
// **SWAP TWO ELEMENT IN AN ARRAY
// const color = ["red","green","blue","yellow","black"];
// [color[0],color[4]] = [color[4],color[0]];
// console.log(color);
// --------------------------example -3
// **assign array element to variables
// const color = ["red","green","blue","yellow","black"];
// [firstcolor,secondcolor,thirdcolor,fourthcolor,fifthcolor] = [color[0],color[1],color[2],color[3],color[4],color[5]]; // process 1
// [firstcolor,secondcolor,thirdcolor,...extracolors] = color;
// console.log(secondcolor);
// console.log(thirdcolor);
// console.log(extracolors);
// ------------------------------Example - 4
//** extract Values from Objects
// const person1 = {
// fname:"Anil",
// lname:"Nayak",
// age:21
// }
// const person2 = {
// fname :"sunil",
// age:32
// }
// const {fname,lname = "panda",age} = person2;
// console.log(fname);
// console.log(lname);
// console.log(age);
//--------------------------- example-5
//----------------------- DESTRUCTURE IN FUCNTION PARAMETERS
// function displayPerson({fname,lname = "nayak",age}){
// console.log(fname);
// console.log(lname);
// console.log(age);
// }
// const person1 = {
// fname:"Anil",
// lname:"Nayak",
// age:21
// }
// const person2 = {
// fname :"sunil",
// age:12,
// }
// displayPerson(person1)
// displayPerson(person2)