-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.js
234 lines (179 loc) · 5.98 KB
/
first.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/************************************ 1. LET **************************************************/
let a = 50;
let b = 100;
if (true) {
let a = 60;
var c = 10;
console.log(a / c); // 6
console.log(b / c); // 10
}
console.log(c); // 10
console.log(a); // 50
/*************************************************************************************************/
/************************************ 2. Const **************************************************/
const a = 50;
a = 60; // shows error. You cannot change the value of const.
const b = "Constant variable";
b = "Assigning new value"; // shows error.
//Consider another example.
const LANGUAGES = ['Js', 'Ruby', 'Python', 'Go'];
LANGUAGES = "Javascript"; // shows error.
LANGUAGES.push('Java'); // Works fine.
console.log(LANGUAGES); // ['Js', 'Ruby', 'Python', 'Go', 'Java']
/*************************************************************************************************/
/************************************ 3. Arrow Function **************************************************/
// Old Syntax
function oldOne() {
console.log("Hello World..!");
}
// New Syntax
var newOne = () => {
console.log("Hello World..!");
}
let NewOneWithParameters = (a, b) => {
console.log(a+b); // 30
}
NewOneWithParameters(10, 20);
//Default Parameters:
let Func = (a, b = 10) => {
return a + b;
}
Func(20); // 20 + 10 = 30
Func(20, 50); // 20 + 50 = 70
let NotWorkingFunction = (a = 10, b) => {
return a + b;
}
NotWorkingFunction(20); // NAN. Not gonna work.
/*************************************************************************************************/
/************************************ 4. For of loop ****************************************/
let arr = [2,3,4,1];
for (let value of arr) {
console.log(value);
}
let string = "Javascript";
for (let char of string) {
console.log(char);
}
/*************************************************************************************************/
/************************************ 5. Spread attributes **************************************/
let SumElements = (arr) => {
console.log(arr); // [10, 20, 40, 60, 90]
let sum = 0;
for (let element of arr) {
sum += element;
}
console.log(sum); // 220.
}
SumElements([10, 20, 40, 60, 90]);
let SumElements = (...arr) => {
console.log(arr); // [10, 20, 40, 60, 90]
let sum = 0;
for (let element of arr) {
sum += element;
}
console.log(sum); // 220.
}
SumElements(10, 20, 40, 60, 90); // Note we are not passing array here. Instead we are passing the elements as arguments.
Math.max(10, 20, 60, 100, 50, 200); // returns 200.
let arr = [10, 20, 60];
Math.max(arr); // Shows error. Doesn't accept an array.
let arr = [10, 20, 60];
Math.max(...arr); // 60
/*************************************************************************************************/
/************************************ 6. Maps **************************************************/
//
var NewMap = new Map();
NewMap.set('name', 'John');
NewMap.set('id', 2345796);
NewMap.set('interest', ['js', 'ruby', 'python']);
NewMap.get('name'); // John
NewMap.get('id'); // 2345796
NewMap.get('interest'); // ['js', 'ruby', 'python']
//
var map = new Map();
map.set('name', 'John');
map.set('name', 'Andy');
map.set(1, 'number one');
map.set(NaN, 'No value');
map.get('name'); // Andy. Note John is replaced by Andy.
map.get(1); // number one
map.get(NaN); // No value
//Other useful methods used in Map:
var map = new Map();
map.set('name', 'John');
map.set('id', 10);
map.size; // 2. Returns the size of the map.
map.keys(); // outputs only the keys.
map.values(); // outputs only the values.
for (let key of map.keys()) {
console.log(key);
}
//
var map = new Map();
for (let element of map) {
console.log(element);
}
//
var map = new Map();
for (let [key, value] of map) {
console.log(key+" - "+value);
}
/*************************************************************************************************/
/************************************ 7. Sets **************************************************/
//
var sets = new Set();
sets.add('a');
sets.add('b');
sets.add('a'); // We are adding duplicate value.
for (let element of sets) {
console.log(element);
}
//
var sets = New Set([1,5,6,8,9]);
sets.size; // returns 5. Size of the size.
sets.has(1); // returns true.
sets.has(10); // returns false.
/*************************************************************************************************/
/************************************ 8. Static methods **************************************************/
//
class Example {
static Callme() {
console.log("Static method");
}
}
Example.Callme();
/*************************************************************************************************/
/************************************ 9. Getters and Setters **************************************************/
//
class People {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
setName(name) {
this.name = name;
}
}
let person = new People("Jon Snow");
console.log(person.getName());
person.setName("Dany");
console.log(person.getName());
//
class People {
constructor(name) {
this.name = name;
}
get Name() {
return this.name;
}
set Name(name) {
this.name = name;
}
}
let person = new People("Jon Snow");
console.log(person.Name);
person.Name = "Dany";
console.log(person.Name);
/*************************************************************************************************/