-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDestructurring.js
More file actions
56 lines (40 loc) · 1.24 KB
/
Destructurring.js
File metadata and controls
56 lines (40 loc) · 1.24 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
//Old ways
const vehicles = ["A car", "A truck", "A Bus"];
const car = vehicles[0];
const truck = vehicles[1];
const bus = vehicles[2];
console.log(car);
console.log(truck);
console.log(bus);
//New Way
const food = ["Agbado", "Garri", "Ewa"];
const [corn, cassava, legume] = food;
console.log(cassava);
const phones = ["iPhone", "Andriod", "Samsung"];
const [Expensive, , LeveleUp] = phones;//middle omitted but comma is still there so it will be known that it was skipped and allows the next be for the next!
console.log(LeveleUp);
//Helpful when a function return value is an array. const dateInfo(dat) = [d,m,y]
function dateInfo(dat) {
const d = dat.getDate();
const m = dat.getMonth() + 1;
const y = dat.getFullYear();
return [d, m, y];
}
//Therefore const
const [date, month, year] = dateInfo(new Date());
console.log(dateInfo(new Date()));
console.log(date);
console.log(month);
console.log(year);
//Destructuring Works on object too
const Person = {
firstName: "Rasheed",
lastName: "Olaniran",
age: 26,
occupation: "moderator"
}
const {firstName, lastname, age, occupation, country = "Nigeria"} = Person;
console.log(country);
console.log(occupation);
//Instead of (Both gives same result)
console.log(Person.occupation);