-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDestructuring.js
More file actions
63 lines (50 loc) · 1.36 KB
/
Copy pathDestructuring.js
File metadata and controls
63 lines (50 loc) · 1.36 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
// Destructuring
// array destructuring
// object destructuring
// Rename variable
// Destructring in variable
// object destructuring
const student = {
name : "afaq",
age : 21
};
// for destructring the key's will become th properties and can be used as variables
const {name , age} = student;
// then made equal to the obejct name
console.log(`the name is : ${name}
and age is : ${age}`);
// Renaming variables
// if you want to change the name of the property or variable other than the key name of object then use this
const student = {
name : "afaq",
age : 21
};
const{name:studntname , age :studentage} = student;
console.log(`the name is : ${studntname}
and age is : ${studentage}`);
// Array Destructring
// Only the brackets change from {} to []
let arr1 = ["Apple","Grapes","pinaple"];
const [first,second,third] = arr1;
console.log(`Fruits are: ${first} ,${second},${third}`);
//Only the brackets are changed and the properties are as numbers
//Nested object Destruction
const student = {
name : "afaq",
age : 21,
marks : {
phy :34,
math :39,
eng : 50
}
};
const {
marks :{
phy,math,eng
}
} = student;
console.log(`The marks are ${phy} , ${math} , ${eng}`);
//Fucntion Destructuring
function PrintStudent ({name,age}){
console.log(`The name is ${name} and the age is ${age}`);
}