-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
787 lines (587 loc) · 13.6 KB
/
Copy pathjavascript.js
File metadata and controls
787 lines (587 loc) · 13.6 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
// JAVA SCRIPRT
Mongo DB, Express, React, Node - All three utilizes javascript
Version: Both js and ts belongs to ES
ES -
dataType: Frist thing to check in any language
String : '' or " " (any thing in a quote is String)
Number : 23, 23.232, 232422424
Boolean : true or false
null
undefined
variable:
defing a variable:
var a = 'ds'
var b = 2244
var c = true
Typeof is inbuilt method in the javascript which tells the typeof variable
modules of smaller to larger is always smaller
2%3 = 2
6%7 = 6
if we add + with string it with Concate
String + string = String
number + number = number
string + number = string
number + string = string
'10'+10+'10'
10+10+'10'
'10'+10+10
'10'+10+'10'-1
10+10+'10'-1
2009
'10'+10+10-1
101009
if both the side is a pure number as string or number with + they will Concate
but with , * / it will do the operation
true is eqaul to 1
false is eqaul to 0
true + true
2
true + 10
11
'hai'+ true
'haitrue'
// comparsion operation
= Assingment
== compare the data
=== compare the data and dataType
JavaScript is caseSensitive
//checking the output and debugging
console.log('this is mydata')
alert('just for notification');
comfirm('do u want to leave?')
ok = true
cancel = false
prompt('to take the user input')
it will take the value which is Submitted by the user
it will return as the string from input box or prompt or anything
converting the string to number ==> parseInt(s)
parseInt will give u pure number
In Es6 Number is new way to conveting to number
//// string operations
These methods are useful for extracting the string from a larger one
///Sub string
it is a subset of another string.
String.substring(index A, index B), end index is optinal
Slice --- it will remove the first element in the string
var city = 'Delhi'
city.slice(1)
elhi
city.slice(1,4)
elh
slice and substring gives the same result
slice(start,end?)
substring(start,end?)
substr(start,length?)
////////// negative operations on strings
var city = 'Delhi'
city.slice(-1) // gives the last character
i
/////////// repalcing the strings
var str = 'this is a javascript'
str.replace(/a/,'js')
"this is js javascript"
str.replace(/a/g,'js')
"this is js jjsvjsscript"
str.replace(/ /g,'')
"thisisajavascript"
//////Math operations////
Math.floor(Math.random()*100000)
42966
Math.floor(Math.random()*100000)
71896
Math.floor(Math.random()*100000)
10929
Math.floor(Math.random()*100000)
84286
multiple no of zeros for getting the random number with digits
floor is on lower side
ceil is on lower side
round is on higher side
// generating the random number between the range
Math.floor(Math.random()*(max-min))+min
///lodcal strorage & session storage & cookies
localStorage - it is a persistant Storage(we need to remove manually)
localStorage.setItem('_ID','314424')
undefined
localStorage.getItem('_ID')
"314424"
localStorage.removeItem('_ID')
undefined
//// SessionStorage
sessionStorage - It is non persistant Storage(tab specifc)
sessionStorage.setItem('_SAFJOFA', '32452143');
undefined
sessionStorage.getItem('_SAFJOFA');
"32452143"
sessionStorage.removetItem('_SAFJOFA');
undefined
///Cookies
it is saved bases on web URL, It is dedicated only to that particular link
it has exipry Time
document.cookie="name=value"
/// string literals
var name = 'Avenger';
var rating = 3.5
var type = 'Action';
var region = 'Hollywood'
//ES5
var data = "This movie name is "+ name +" has a rating of "+rating+" and belongs to "+type+" "
//ES6
var data = `the movie name is ${name} has a rating od ${rating} and belongs to ${region}`
////If condition
if(conditon){
//logic
}else{
//logic
}
var a = 46324524551
if(a%2==0) {
console.log(`${a} is an even number);
}else{
alert(${a} is an odd number`);
}
////Ternary operator
> Its a single line if else condition
a= 10
a=10 ? a : b
var y =9
y>10? y+1 : y-1
8
//// truthy and falsy
truthy = true & 1 which is either positive or negative, except 0
falsy = false & 0 , null, undefined
/// date in javascript
Date()
"Mon Mar 22 2021 21:34:45 GMT+0530 (India Standard Time)"
let a = Date()
a.substr(0,15)
"Mon Mar 22 2021"
a.substr(16,8)
"21:35:00"
var b = new Date()
b.getDate()
22
b.getFullYear()
2021
b.getTime()
1616429417902
sunday is 0
january is 0
////Switch
var name = 'Sandeep'
undefined
switch(name){
case 'Sandeep':
console.log('You are a user');
break;
case 'Deepu':
console.log('You are an Admin');
break;
default :
console.log('You dont have any privilages');
}
You are a user
undefined
var name = 'adad'
undefined
switch(name){
case 'Sandeep':
console.log('You are a user');
break;
case 'Deepu':
console.log('You are an Admin');
break;
default :
console.log('You dont have any privilages');
}
You dont have any privilages
if we don't specify break, it will pirnt the true conditon and below one's too
// number is odd or even using the swith case
var num = 3.2;
switch(num%2) {
case 0:
console.log('number is even');
break;
case 1:
console.log('number is odd');
break;
default:
console.log('number is neither even nor odd')
}
// Regular expression
-It is use to match the pattern
starts with ^ and ends with $ (^$)
("^([a-z])$")
var t = 'r'
t.match("^([a-zA-Z0-9])$")
t.match("^([a-z]{2})$")
(2) ["r", "r", index: 0, input: "r", groups: undefined]
[] - It matches the pattern for the first Character
{1} - It matches the first CharacterData
{2,4} - min 2 and max 4
+ - It matches for any length
It should start with number and rest any thing else
data.match("^([1-9]+)([a-zA-Z]+)$")
/////Arrays
It is a collection of homogenous data
var a = ['a','23','daad']
var b = [1,3,4,5,3]
var c = [true,false,true]
But in JavaScript and python
It is a collection of homogenous and hetrogenous
var d = ['exmaple',23,false];
var city = ['Delhi','Hyderabad','Mumbai']
typeof(city) is an object
object
var city = ['Delhi','Texas','Newyork']
undefined
typeof(city)
"object"
city.length
3
city[2]
"Newyork"
city[city.length-1]
"Newyork"
city.push('SanJose')
4
city
(4) ["Delhi", "Texas", "Newyork", "SanJose"]
city.push('extra')
5
city
(5) ["Delhi", "Texas", "Newyork", "SanJose", "extra"]
city.pop()
"extra"
city
(4) ["Delhi", "Texas", "Newyork", "SanJose"]
pop(3) - pop with any index value is not counted, it only removes lst element
pop will remove at beigning
push will add at end
unshift will add at beigning
shift will remove always from beigning
city.unshift('one')
5
city
(5) ["one", "Delhi", "Texas", "Newyork", "SanJose"]
city.unshift()
5
city
(5) ["one", "Delhi", "Texas", "Newyork", "SanJose"]
city.unshift('two')
6
city
(6) ["two", "one", "Delhi", "Texas", "Newyork", "SanJose"]
//////////////functions//////////////////
function greet() {
console.log('Normal function')
}
greet()
o/p Normal function
we need to call the function explictly to generate the output
///IIFE//// Immediately invokable functions
(function(){
console.log('calling IIFE')
}())
this function will be executed on the page load without calling explicitly
////genarator function/////
we will control the flow of the function output when to show the value
//without control
function random(userInput) {
for (i=0;i<userInput; i++)
console.log(i)
}
random(4)
0
1
2
3
//with control
function * alpha(x) {
for(i=0;i<x;i++){
yield i;
}
}
var date = alpha(4)
data.next() // it will load the data when in need
{value: 0, done: false}done: falsevalue: 0[[Prototype]]: Object
data.next()
{value: 1, done: false}
data.next()
{value: 2, done: false}
data.next()
{value: 3, done: false}
data.next()
{value: 4, done: false}
data.next()
{value: undefined, done: true}
/////////////// Map & Filter //////////////
var a = [1,234,433,54,3,4,546,65,737,67,54,6,3634,364]
undefined
a.map((data) => {return data})
(14) [1, 234, 433, 54, 3, 4, 546, 65, 737, 67, 54, 6, 3634, 364]
let's multiply the input array by 2
a.map((data) => {console.log(data*2)})
(14) [1, 234, 433, 54, 3, 4, 546, 65, 737, 67, 54, 6, 3634, 364]
VM589:1 2
VM589:1 468
VM589:1 866
VM589:1 108
VM589:1 6
VM589:1 8
VM589:1 1092
VM589:1 130
VM589:1 1474
VM589:1 134
VM589:1 108
VM589:1 12
VM589:1 7268
VM589:1 728
map will iterate and return the array...
> it will always return the same length as input array
> it is used to apply the logic as sum, multiply ..etc
FILTER:
it is use to filter the values from array.
> it may or maynot retutn the same length as input array
> it only returns those value which ouptut is true
var b=[5,11,3,6,8,23,24,15,22,13,10]
b.filter((data) => {return data >10})
(6) [11, 23, 24, 15, 22, 13]
0: 11
1: 23
2: 24
3: 15
4: 22
5: 13
b.map((data) => {return data >10})
(11) [false, true, false, false, false, true, true, true, true, true, false]
tricky q
var a=[0,1,2,3]
a.map((data) => { return data*2 })
(4) [0, 2, 4, 6]
var a=[0,1,2,3]
a.filter((data) => { return data*2 })
(3) [1, 2, 3]
//////let & var
var > we can able to redeclare and reassign
let > we cannot able to redeclare and can reassign
const > we cannot redeclare and reassign
////clouser ///
clouser is something where we can close or restrict the scope
var a =10; //global scope
function add() {
var b=20; // local scope
return a+b;
}
console.log("ADD", add());
console.log("value of a", a);
console.log("value of b", b);
o/p
"ADD"
30
"value of a"
10
"error"
"ReferenceError: b is not defined
{} will consider as clouser only in the functions but not in loops.
var a =10;
for (i=0; i<10; i++) {
var b=20;
console.log(a+b+i);
}
console.log("value of a", a);
console.log("value of b", b);
30
31
32
33
34
35
36
37
38
39
"value of a"
10
"value of b"
20
let helps to restrict the scope...
///// hoisting ////
we can call the function even before the declaration
var a
undefined
typeof(a)
undefined
var b = null
typeof(b)
object
///////////////// Object ///////////////////
Single object can have n number of properties
var movies = {
name : 'avengers',
rating: 3.5,
lang: 'english'
}
undefined
typeof(movies)
'object'
//// dot notation
movies.name
'avengers'
movies.lang
'english'
movies.rating
3.5
movies.rating = 3.9
3.9
movies
{name: 'avengers', rating: 3.9, lang: 'english'}
///// bracket notation
movies["lang"]
'english'
> we can access the properties
> we can add a propety
> we can modify the property
> we can delete the property
JSON - having multiple objects in a array
> a way to reprsent the data
> a key value pair of the data
var movies = [
{
name : 'avengers',
rating: 3.5,
lang: 'english'
},
{
name : 'teal',
rating: 3.5,
lang: 'english'
},
{
name : 'dil',
rating: 4.5,
lang: 'telugu'
}
]
movies
(3) [{…}, {…}, {…}]
0: {name: 'avengers', rating: 3.5, lang: 'english'}lang: "english"name: "avengers"rating: 3.5[[Prototype]]: Object1: {name: 'teal', rating: 3.5, lang: 'english'}2: {name: 'dil', rating: 4.5, lang: 'telugu'}length: 3[[Prototype]]: Array(0)
movies.name
undefined
movies[1].rating
3.5
movies[0].name
'avengers'
*****************
when a function is assigned to a variable it become method
we can call multiple times
var calc = {
sum:function(a,b) { return a+b }
sub: (a,b) => { return a-b }
}
undefined
calc.sum(3,5)
8
calc.sub(10,3)
7
var dbquery = {
find:(table) => { return `select * from ${table}` }
}
undefined
dbquery.find('student')
'select * from student'
////////// This ///////
this keyword refers to the above scope
var lastName = 'Scott'
var sayHi = {
firstName: 'zoe',
lastName: 'Lisa',
people:function() {
return `say hi to ${this.firstName} ${this.lastName}`
}
}
console.log(sayHi.people())
o/p
"say hi to zoe Lisa"
///// defining an object using construtor notation //////
var bob = new Object(); ///object bob is created with no properties
bob.age = 29 // assigned age property
obj.setAge=function(newAge){
this.age= newAge;
}
////
var createAge = function(newAge) {
this.age = newAge
}
var bob = new Object();
bob.age = 29
bob.city = 'delhi'
bob.setAge = createAge
var tony = new Object();
tony.age = 29
tony.city = 'delhi'
tony.setAge = createAge
o/p
ƒ (newAge) {
this.age = newAge
}
bob
{age: 29, city: 'delhi', setAge: ƒ}
bob.setAge(28)
undefined
bob
{age: 28, city: 'delhi', setAge: ƒ}
//////////////// functions as an object ////////////////
function language(name,country){
this.name = name;
this.country = country;
}
undefined
var hindi = new language('hindi','india') // creating obj using constructor
undefined
hindi // hindi is a obj having name and country as a properties
language {name: 'hindi', country: 'india'}
/// in es6 we can do by using classes
class language {
constructor(name.country) {
this.name = name;
this.country = country;
}
}
var eng = new language('English','USA')
//////// prototpyes/////////////
//in es5
function human(name) { /// one arg as name
this.name = name;
this.city = 'london';
}
function robot(name) {.
this.name = name;
this.legs = 2;
this.color = 'red'
}
human.prototype = new robot() //acessing the robot properties to the human
var john = new human ('joy')
robot {name: undefined, legs: 2, color: 'red'}
var chitti = new robot('robo')
undefined
chitti
robot {name: 'robo', legs: 2, color: 'red'}
john.color /// human can stil access the propeties of robot
'red'
john.legs
2
//// using es6
class geo {
constructor(lan,lon) {
this.lan = 11.1331;
this.lon = 213.242;
}
}
class human extends geo { ///// we will use extends keyword to inhert the properties from geo
constructor(name,country,lan,lon) { //// we should declare the geo arg in constructor
super(lan,lon) /// to differentiate geo properties we will place in super
this.name = name;
this.country =country;
}
}