-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdestructure.js
More file actions
39 lines (32 loc) · 778 Bytes
/
destructure.js
File metadata and controls
39 lines (32 loc) · 778 Bytes
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
// Ex - 1
const [jan, feb, mar, , may] = [10, 20, 20, 40, 50];
console.log(jan); // 10
console.log(may); // 50
// Ex - 2 = Rest Operator
const [month1, ...otherMonths] = [10, 20, 30, 40, 50];
console.log(month1); //10 - Assign 10 for month1
console.log(otherMonths); // 20, 30, 40, 50
// Ex - 3
const data = {
name: "Safnaj",
city: "MRM",
country: "Sri Lanka",
age: 25,
vehicile: "Swift",
}
let { name, city, ...person } = data;
console.log(person); //Prints country, age, vehicle
let person2 = {
...data, //copying object
email: "safnaj99@live.com"
}
console.log(person2);
//prints
// {
// name: 'Safnaj',
// city: 'MRM',
// country: 'Sri Lanka',
// age: 25,
// vehicile: 'Swift',
// email: 'safnaj99@live.com'
// }