-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-js.html
More file actions
373 lines (271 loc) · 9.79 KB
/
basic-js.html
File metadata and controls
373 lines (271 loc) · 9.79 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Class</h2>
<p>In this example we demonstrate a simple class definition and how to use it.</p>
<code>
class Car {
constructor(brand) {
this.carname = brand;
}
}
class Motor {
constructor(name) {
this.motorname = name;
}
}
mymotor = new Motor("General Motor");
mycar = new Car("Ford");
document.getElementById("demo").innerHTML = mymotor.motorname;
</code>
<p id="demo"></p>
<script>
class Car {
constructor(brand) {
this.carname = brand;
}
}
class Motor {
constructor(name) {
this.motorname = name;
}
}
mymotor = new Motor("General Motor");
mycar = new Car("Ford");
document.getElementById("demo").innerHTML = mymotor.motorname;
</script>
<h2>Array.find()</h2>
<p id="demo2"></p>
<script>
var numbers = [4, 9, 16, 25, 29];
var first = numbers.find(myFunction);
document.getElementById("demo2").innerHTML = "First number over 18 is " + first;
function myFunction(value, index, array) {
return value > 18;
}
</script>
<h2>Number Object Properties</h2>
<p>MIN_SAFE_INTEGER</p>
<p id="demo3"></p>
<p>MAX_SAFE_INTEGER</p>
<p id="demo4"></p>
<script>
var y = Number.MIN_SAFE_INTEGER;
var x = Number.MAX_SAFE_INTEGER - 1;
document.getElementById("demo4").innerHTML = x;
document.getElementById("demo3").innerHTML = y;
</script>
<h2>JavaScript Number Methods</h2>
<p>The isInteger() method returns true if the argument is an integer.</p>
<p>Otherwise it returns false.</p>
Number.isInteger(10) + "<br>" + Number.isInteger(10.5);
<p id="demo5"></p>
<script>
document.getElementById("demo5").innerHTML =
Number.isInteger(10) + "<br>" + Number.isInteger(10.5);
</script>
<h2>JavaScript Number Methods</h2>
<p>The isSafeInteger() method returns true if the argument is a safe integer.</p>
<p>Otherwise it returns false.</p>
Number.isSafeInteger(10) + "<br>" + Number.isSafeInteger(12345678901234567890);
<p id="demo6"></p>
<script>
document.getElementById("demo6").innerHTML =
Number.isSafeInteger(10) + "<br>" + Number.isSafeInteger(12345678901234567890);
</script>
<h2>JavaScript Number Methods</h2>
<p>The isFinite() method returns false if the argument is Infinity or NaN.</p>
<p>Otherwise it returns true.</p>
isFinite(10 / 0) + "<br>" + isFinite(10 / 1);
<p id="demo7"></p>
<script>
document.getElementById("demo7").innerHTML =
isFinite(10 / 0) + "<br>" + isFinite(10 / 1);
</script>
<h2>The ** Operator</h2>
<p id="demo8"></p>
<script>
var x = 10;
document.getElementById("demo8").innerHTML = x ** 2;
</script>
<h2>JavaScript Array.forEach()</h2>
<p>Calls a function once for each array element.</p>
var txt = "";</br>
var numbers = [45, 4, 9, 16, 25];</br>
numbers.forEach(myFunction);</br>
document.getElementById("demo10").innerHTML = txt;</br>
function myFunction(value) {</br>
txt = txt + value + "<br>"; }
</br>
<p id="demo10"></p>
<script>
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);
document.getElementById("demo10").innerHTML = txt;
function myFunction(value) {
txt = txt + value + "<br>";
}
</script>
<h2>JavaScript Array.filter()</h2>
<p>Creates a new array with all array elements that passes a test.</p>
<p id="demo12"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var over18 = numbers.filter(myFunction);
document.getElementById("demo12").innerHTML = over18;
function myFunction(value, index, array) {
return array;
}
</script>
<h2>JavaScript Array.reduce()</h2>
<p>This example finds the sum of all numbers in an array:</p>
var sum = numbers.reduce(myFunction)<br> document.getElementById("demo").innerHTML = "The sum is " + sum
<br> function myFunction(total, value, index, array) {<br> return total + value<br> }
<br>
<p id="demo13"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var sum = numbers.reduce(myFunction);
document.getElementById("demo13").innerHTML = "The sum is " + sum;
function myFunction(total, value, index, array) {
return total + value;
}
</script>
<h2>JavaScript Array.every()</h2>
<p>The every() method checks if all array values pass a test.</p>
<code>
var numbers = [45, 4, 9, 16, 25];
var allOver18 = numbers.every(myFunction);
document.getElementById("demo14").innerHTML = "All over 18 is " + allOver18;
function myFunction(value, index, array) {
return value > 18;
}
</code>
<p id="demo14"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var allOver18 = numbers.every(myFunction);
document.getElementById("demo14").innerHTML = "All over 18 is " + allOver18;
function myFunction(value, index, array) {
return value > 18;
}
</script>
<h2>JavaScript Array.indexOf()</h2>
<p id="demo15">/p>
<script>
var fruits = ["Apple", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Orange");
document.getElementById("demo15").innerHTML = "Orange is found in position " + a;
</script>
<h2>Create Object from JSON String</h2>
<code>
var txt = '{"name":"John", "age":30, "city":"New York"}'
var obj = JSON.parse(txt);
document.getElementById("demo16").innerHTML = obj.name + ", " + obj.age;
</code>
<p id="demo16"></p>
<script>
var txt = '{"name":"John", "age":30, "city":"New York"}'
var obj = JSON.parse(txt);
document.getElementById("demo16").innerHTML = obj.name + ", " + obj.age;
</script>
<h2>Create JSON string from a JavaScript object.</h2>
<code>
var obj = {
name: "John",
age: 30,
city: "New York"
};
var obj2 = {
name: "sunny",
age: 30,
city: "NY"
};
var myJSON = JSON.stringify(obj2);
document.getElementById("demo17").innerHTML = myJSON;
</code>
<p id="demo17"></p>
<script>
var obj = {
name: "John",
age: 30,
city: "New York"
};
var obj2 = {
name: "sunny",
age: 30,
city: "NY"
};
var myJSON = JSON.stringify(obj2);
document.getElementById("demo17").innerHTML = myJSON;
</script>
<h2>ES6 and Variable Hoisting</h2>
<code>
var main = function() {
for (var x = 0; x < 5; x++) {
console.log(x);
}
console.log("x can be accessed outside the block scope x value is :" + x);
console.log('x is hoisted to the function scope');
document.getElementById("demo18").innerHTML = x;
}
main();
</code>
<p id="demo18"></p>
<script>
var main = function() {
for (var x = 0; x < 5; x++) {
console.log(x);
}
console.log("x can be accessed outside the block scope x value is :" + x);
console.log('x is hoisted to the function scope');
document.getElementById("demo18").innerHTML = x;
}
main();
</script>
<h2>No binding of arguments
</h2>
<p id="demo19"></p>
<script>
function foo(n) {
var f = (...args) => args[0] + n;
return f(10);
}
num = foo(1); // 11
document.getElementById("demo19").innerHTML = num;
console.log(num);
</script>
<h2>Arrow functions used as methods</h2>
<p>As stated previously, arrow function expressions are best suited for non-method functions. Let's see what
</p>
'use strict';<br> var obj = { // does not create a new scope<br> i: 10,<br> b: () => console.log(this.i, this),<br> c: function() {<br> console.log(this.i, this);<br> }
<br> }
<br>
<br> obj.b(); // prints undefined, Window {...} (or the global object)<br> obj.c(); // prints 10, Object {...}<br>
<script>
'use strict';
var obj = { // does not create a new scope
i: 10,
b: () => console.log(this.i, this),
c: function() {
console.log(this.i, this);
}
}
obj.b(); // prints undefined, Window {...} (or the global object)
obj.c(); // prints 10, Object {...}
</script>
<h2>The Function Constructor
</h2>
var func = new Function("x", "y", "return x*y;"); <br> function product() { <br> var result; <br> result = func(10,20); <br> console.log("The product : "+result)<br> } product()
<script>
var func = new Function("x", "y", "return x*y;");
function product() {
var result;
result = func(10, 20);
console.log("The product : " + result)
}
product();
</script>
</body>
</html>