-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17-ES6+Modern-JavaScript-Features.js
More file actions
158 lines (116 loc) · 2.74 KB
/
17-ES6+Modern-JavaScript-Features.js
File metadata and controls
158 lines (116 loc) · 2.74 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// ===============================
// let vs const
// ===============================
let count = 1;
console.log(count); // 1
count = 2;
console.log(count); // 2
const PI = 3.14;
console.log(PI);
// Not allowed (const cannot be reassigned)
// PI = 3.15;
// console.log(PI);
// ===============================
// const with objects
// ===============================
const user = { name: "JD" };
console.log(user);
// Allowed: modifying object properties
user.name = "JDCodebase";
console.log(user);
// Not allowed: reassigning object
// user = {};
// ===============================
// const with arrays
// ===============================
const arr = [10, 20, 30];
console.log(arr);
// Allowed: modifying array elements
arr[2] = 50;
console.log(arr);
// ===============================
// Normal function
// ===============================
function add(a, b) {
return a + b;
}
console.log(add(3, 4)); // 7
// ===============================
// Arrow function
// ===============================
const add1 = (a, b) => a + b;
console.log(add1(5, 6)); // 11
const greet = (name) => {
return `Hello ${name}`;
};
console.log(greet("JD"));
// ===============================
// this in normal function
// ===============================
function show() {
console.log(this);
}
show(); // global object (Node.js)
// ===============================
// this inside object
// ===============================
const user1 = {
name: "JD",
age: 25,
// Normal function (method)
sayHello: function () {
console.log("Hey");
console.log(this); // refers to user1
},
// Arrow function
show: () => {
console.log("arrow function");
console.log(this); // NOT user1
},
};
user1.sayHello();
user1.show();
// ===============================
// Arrow function at top level
// ===============================
const arrowFunc = () => {
console.log(this);
};
arrowFunc(); // {}
// ===============================
// Spread operator (arrays)
// ===============================
const nums = [10, 20, 30];
console.log(nums);
const newNums = [...nums, 40, 50];
console.log(newNums);
// ===============================
// Spread operator (objects)
// ===============================
const user3 = {
name: "JD",
};
const updatedUser = {
...user3,
role: "admin",
};
console.log(user3);
console.log(updatedUser);
// ===============================
// Rest operator
// ===============================
function sum(...numbers) {
return numbers.reduce((a, b) => a + b);
}
console.log(sum(1, 2, 3, 4)); // 10
// ===============================
// Object destructuring
// ===============================
const user4 = {
name: "JD",
age: 25,
role: "dev",
};
// Rename while destructuring
const { name: username, role } = user4;
console.log(username, role);