-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava Script.txt
More file actions
754 lines (685 loc) · 19.9 KB
/
Java Script.txt
File metadata and controls
754 lines (685 loc) · 19.9 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
Prerequisite
*Basic computer knowledge
*Understanding Of HTML And CSS
*Text editor(Visual studio code, Notepad, etc)
*Web browser(Google chrome, Microsoft edge, etc)
In this section we are going to see complete JavaScript Course:
1. Introduction 2. Operators 3. Array 4. Conditional statements 5. Variables 6. Comparison operators 7. JS for loop
8. JS Iteration statement 9. Do..while loop 10. Cookies 11. Functions 12. Error handling 13. Events 14. While loop 15. Page redirect
16. JS Loop control statements 17. Logical operators 18. Switch statements 19. Objects 20. JS If else statements 21. Animation.
1. Introduction to JavaScript
JavaScript Introduction
`Client-Side Scripting Language
`It is untyped, multi paradigm, functional, event driven
`It is Interpreted by browser's javascript engine.
JavaScript Advantages
^Less Server Interactions
^Better Performance
^Richer Interface
^Increased Interactivity
2. JavaScript Operators
Operators Introduction
`It is a symbol, which tells the system to perform specific operation
`Javascript has rich collection of built-in data types
`Operators can be unary, binary or ternary.
Operators classification
^Arithmatic Operator-Refer(Arithmetic operators.jpg)
^Assignment Operator
^Comparison Operator
^Logical Operator
^Ternary Operator
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var num1=40, num2=10;
document.write(num1 + num2);
document.write("<br/>");
document.write(num1 - num2);
document.write("<br/>");
document.write(num1 * num2);
document.write("<br/>");
document.write(num1 / num2);
document.write("<br/>");
document.write(num1 % num2);
document.write("<br/>");
</script>
or
<script language="javascript" type="text/javascript">
var num1=40, num2=10;
var result = num1 + num2;
document.write(result);
or
num1=num1+5;
document.write(num1);
or
var result=10+5*20; =110 or (10+5)*20;=300 parenthesis
document.write(result);
</script>
</body>
</html>
#Pre & post increment method Pre & post decrement method (++num & --num)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var num=10;
var result=++num; or num++;
document.write(result);
document.write("<br/>");
document.write(num);
</script>
</body>
</html>
3. JavaScript Arrays
Lets you store multiple values in a single variable
It stores a fixed size sequential collection of elements
To access any element, we need to pass the index
Writing Data
var fruits = new Array ("apple", "orange", "mango");
var fruits = ["apple", "orange", "mango"];
Reading Data
fruits [0] is the first element
fruits [1] is the second element
fruits [2] is the third element
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var arr = new Array(5);
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (i = 0; i<5; i++) {
document.write(arr[i] + "<br/>")
}
</script>
or
<script language="javascript" type="text/javascript">
var arr = new Array(10,20,30,40,50);
for (i = 0; i<5; i++) {
document.write(arr[i] + "<br/>")
}
</script>
</body>
</html>
2imp tasks in Array are sorting(ascending order > & < descending order) & searching
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var arr = new Array(7,2,8,1,3,4);
var i, j, temp;
document.write("<br/><br/>Before sort...<br/>");
for (i = 0; i<6; i++) {
document.write(arr[i] + "<br/>");
}
for (i = 0; i<6; i++) {
for (j = 0; j<5; j++) {
if (arr[j] < arr[j+1]) { imp (> <)
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
document.write("<br/><br/>After sort...<br/>");
for (i = 0; i<6; i++) {
document.write(arr[i] + "<br/>");
}
</script>
</body>
</html>
4. Javascript Conditional Statements
Introduction: If... Else... Switch(conditional satisfied by variables & operators of given tasks)
5. Javascript Variables
Variables Introduction
Used to store data temporarily
Variables will not have specific data type
Variables can have global or local scope
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var name = "surya";
var age = 21;
var salary = 50000.11;
document.write("Name : " +name +"<br/>");
document.write("age : " +age +"<br/>");
document.write("salary : " +salary +"<br/>");
</script>
</body>
</html>
6. Javascript Comparison Operators: Refer(Comparison Operators.jpg)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var num1 = 100;
document.write(num1 == 10);
document.write("<br/>");
document.write(num1 != 10);
document.write("<br/>");
document.write(num1 >= 10);
document.write("<br/>");
document.write(num1 <= 10);
document.write("<br/>");
document.write(num1 > 10);
document.write("<br/>");
document.write(num1 < 10);
document.write("<br/>");
</script>
</body>
</html>
7. Javascript For Loop: Refer(For Loop.jpg)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var i;
for(i=1; i<=100; i++) {
if(i % 2 == 0) {
document.write("i = " + i);
document.write("<br/>");
}
}
</script>
or
<script language="javascript" type="text/javascript">
var i;
var sum = 0;
for(i=1; i<=100; i++) {
if(i % 2 == 0) {
sum = sum + i;
}
}
document.write("Total: " + sum);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var i;
var product=1;
var num = parseInt(prompt("Enter a number","Enter..."))
for(i=1; i<=num; i++) {
product=product * i;
}
document.write("Factorial: " + product);
</script>
</body>
</html>
8. JavaScript Iteration Statements Refer(IS Flowchart.jpg)
Iteration Statements Introduction:
Executes set of statements several times
Keeps on executing until gives condition is true
Different control statements can be nested.
Loops Classification: For, While, Do... While
9. JavaScript Do..while loop: Refer(Do..While Loop.jpg)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var actualpin = "3456";
var pin;
var count = 0;
do {
pin = prompt("Enter your pin...", "Enter...");
count++;
}
while (pin != actualpin && count < 3);
if (pin == actualpin ){
document.write("Welcome User");
}
else {
document.write("Account Locked");
}
</script>
</body>
</html>
10. JavaScript Cookies
Is used for maintaining state on client
Cookies are stored on the client's machine as text file
When user visits again, the website reads the data from the cookie.
Cookies Types: Session cookie & Persistent cookie
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
<script language="javascript" type="text/javascript">
function WriteCookie() {
if (document.myform.customer.value == "") {
alert("Enter some value");
return;
}
var cookievalue = escape(document.myform.customer.value);
document.cookie = "name" + cookievalue;
document.write("Setting Cookies : " + "Name = " + cookievalue);
}
</script>
</head>
<body>
<form name="myform" action= "">
Enter name: <input type="text" name="customer">
<input type="button" value="set cookie" onclick="WriteCookie();"/>
</form>
</body>
</html>
11. JavaScript Functions
Functions Introduction
Is a set of statements perfrom specific task
Can be invoked whenever code execution is required
Provides reusability
Makes the code moduler
Can pass values through parameters
Can return value from a function using return keyword.
#Invoking
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
<script language="javascript" type="text/javascript">
function sayHello() {
document.write("Hello there!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type="button" onclick="sayHello()" value="say Hello">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
<script language="javascript" type="text/javascript">
function sayHello(name, age) {
//if (name == "" || age == "")
//return;
document.write(name+" is " + age + " years old.");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input name="t1" placeholder="Enter Name"/> <br/>
<input name="t2" placeholder="Enter Age"/> <br/>
<input type="button" onclick="sayHello(t1.value, t2.value)" value="Say Hello"/>
</form>
</body>
</html>
12. JavaScript Error handling
Runtime errors can be handled
Also called exceptions
Syntax or logical errors can not be handled in runtime
Try, catch, finally and throw are keywords used for exception handling.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
<script language="javascript" type="text/javascript">
function myFunc() {
var a = 100
try {
//var s = a.toUpperCase();
alert("value of a variable is: " + a);
}
catch (e) {
alert("Error");
}
finally {
alert("Finally block will always execute!");
}
}
</script>
</head>
<body>
<p> Click the following button to see result: </p>
<form>
<input type="button" value="Click Me" onclick="myFunc();"/>
</form>
</body>
</html>
13. JavaScript Events
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
<script language="javascript" type="text/javascript">
function MouseOver() {
document.images["img1"].src = "logo red.png"
}
function MouseOut() {
document.images["img1"].src = "logo green.png"
}
function Load() {
document.images["img1"].src = "logo green.png"
}
</script>
</head>
<body onload="Load()">
<img name="img1" onmouseover="MouseOver()"onmouseout="MouseOut()">
</body>
</html>
14. JavaScript While loop Refer(While loop.jpg)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var num = parseInt(prompt("Enter the number", "Enter...")); //5432
var count = 0;
while (num > 0) {
num = Math.trunc(num / 10); //0
count++; //4
}
document.write("Digits: " + count);
</script>
</body>
</html>
15. JavaScript Page redirect
Page Redirect Introduction: Windows.location() method can be used to redirect user from one page to another.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
<script language="javascript" type="text/javascript">
function Redirect() {
window.location = "http://www.Udemy.com";
}
</script>
</head>
<body>
<p>Click the following button, you will be redirect to udemy page</p>
<form>
<input type="button" value="Redirect Me" onclick="Redirect();"/>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
<script language="javascript" type="text/javascript">
function Redirect() {
window.location = "http://www.Udemy.com";
}
</script>
</head>
<body onload="setTimeout('Redirect()',5000)">
<p>You will be redirected in 5 seconds</p>
</body>
</html>
16. JavaScript Loop control statements: Its types are[Break & Continue Statement]
Refer: Break statement for(Break.jpg) and Continue statement for(Continue.jpg)
#Normal.S
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var i;
for (i=1; i<=10; i++) {
document.write("i=" + i + "<br/>");
}
</script>
</body>
</html>
#Break.S
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var i;
for (i=1; i<=10; i++) {
if (i==9)
break;
document.write("i=" + i + "<br/>");
}
</script>
</body>
</html>
#Continue.S
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var i;
for (i=1; i<=10; i++) {
if(i==5 || i==7)
continue;
document.write("i=" + i + "<br/>");
}
</script>
</body>
</html>
17. JavaScript Logical operators: Refer(Logical operators.jpg)
Turnary operator : ? & :
#Logical AND operator
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var uid = prompt("Enter User ID", "User ID");
var pwd = prompt("Enter Password", "Password");
var message = (uid == "SURYA" && pwd == 1111) ? "Welcome" : "Invalid User"
alert(message);
</script>
</body>
</html>
#Logical OR operator
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var user = prompt("Enter your name...", "Name...");
var message = (user == "SURYA" || user == "El") ? "Welcome" : "Invalid User"
alert(message);
</script>
</body>
</html>
#Logical NOT operator: reverse of or operator
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var user = prompt("Enter your name...", "Name...");
var message = !(user == "SURYA" || user == "El") ? "Welcome" : "Invalid User"
alert(message);
</script>
</body>
</html>
18. JavaScript Switch statements: Refer(Switch Flowchart.jpg)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var ch = prompt("Enter a character","Enter....");
switch(ch) {
case 'a':
document.write("A is vowel");
break;
case 'e':
document.write("E is vowel");
break;
case 'i':
document.write("I is vowel");
break;
case 'o':
document.write("O is vowel");
break;
case 'u':
document.write("U is vowel");
break;
default:
document.write("Special character/Consonant")
break;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var ch = prompt("Enter a character","Enter....");
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
document.write(ch + " is vowel");
break;
default:
document.write(ch + " Special character or Consonant")
break;
}
</script>
</body>
</html>
19. JavaScript Objects
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var tutorial = {
"topic": "JavaScript",
"trainer": "Vivek",
"mode": "Online"
};
document.write("Topic : " + tutorial.topic);
document.write("<br/> <br/>");
document.write("Trainer : " + tutorial.trainer);
document.write("<br/> <br/>");
document.write("Mode : " + tutorial.mode);
document.write("<br/> <br/>");
add
var txt = "";
var x;
for (x in tutorial) {
txt += tutorial[x] + " , ";
}
document.write(txt);
</script>
</body>
</html>
20. JavaScript If else statement: Refer(If. Else. Flowchart.jpg)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var marks1 = parseInt(prompt("Enter marks1", "Enter"));
var marks2 = parseInt(prompt("Enter marks2", "Enter"));
var marks3 = parseInt(prompt("Enter marks3", "Enter"));
var total = marks1 + marks2 + marks3;
var percent = total * 100 / 150;
alert(percent);
if (percent >= 80) {
alert("Grade A");
}
else if (percent >= 60) {
alert("Grade B");
}
else if (percent >= 40) {
alert("Grade C");
}
else {
alert("Grade D");
}
</script>
</body>
</html>
21. JavaScript Animation: Basic Animation code
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
<script>
var arr = new Array("1.png", "2.png", "3.png", "4.png");
var i = 1;
var ref;
function picLibrary() {
document.images[0].src = arr[i];
i++
if (i < 3)
i = 0;
}
function start() {
ref = setInterval("picLibrary()", 1000);
}
function stop() {
clearInterval(ref);
}
</script>
</head>
<body onload = "start()">
<img src="1.png" name="img1" onmouseover="stop()" onmouseout="start()"/>
</body>
</html>