-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.html
868 lines (724 loc) · 36.3 KB
/
index.html
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Notes</title>
<link rel="stylesheet" href="base.css">
<!-- <script src="https://use.fontawesome.com/ef12385584.js"></script> -->
<script src="menu.js" type="text/javascript"></script>
</head>
<body>
<div id="wrapper">
<ul class="tab" id="ID" onmouseover="this.style.overflow='auto'" onmouseout="this.style.overflow='hidden'" >
<a onclick="openIntroductionPage()"><img src="images/logo.png"></a>
<!--<a href="http://www.codingninjas.in/"><img src="images/logo.png"></a>-->
<li onclick="openPage(event, 'introduction')" class="first"><a href="javascript:void(0)" class="tablinks">Introduction</a></li>
<li onclick="openPage(event, 'variables-and-data-types')"><a href="javascript:void(0)" class="tablinks">Variables & Data Types</a></li>
<!-- <li onclick="openPage(event, 'data-types')"><a href="javascript:void(0)" class="tablinks">Datatypes</a></li> -->
<li onclick="openPage(event, 'operators')"><a href="javascript:void(0)" class="tablinks">Operators</a></li>
<!-- <li onclick="openPage(event, 'logical-operators')"><a href="javascript:void(0)" class="tablinks">Logical Operators</a></li>
<li onclick="openPage(event, 'conditional-operators')"><a href="javascript:void(0)" class="tablinks">Conditional Operators</a></li>-->
<li onclick="openPage(event, 'for-while')"><a href="javascript:void(0)" class="tablinks">For, While</a></li>
<li onclick="openPage(event, 'functions')"><a href="javascript:void(0)" class="tablinks">Functions</a></li>
<li onclick="openPage(event, 'array')"><a href="javascript:void(0)" class="tablinks">Array</a></li>
<li onclick="openPage(event, 'closures')"><a href="javascript:void(0)" class="tablinks">Closures</a></li>
<li onclick="openPage(event, 'objects')"><a href="javascript:void(0)" class="tablinks">Objects</a></li>
<li onclick="openPage(event, 'prototypes')"><a href="javascript:void(0)" class="tablinks">Prototypes</a></li>
</ul>
<link href="https://fonts.googleapis.com/css?family=Open Sans" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=PT Mono" rel="stylesheet" type="text/css">
<!-- <div id="default" class="default-page">
<h3>Getting Started With</h3>
<h3 id="hidden-heading">HTML and CSS</h3>
<img src="images/html.jpg">
<p>powered by</p>
<a href="http://www.codingninjas.in/"><img src="images/logo.png"></a><br><br>
<a id="jump-to-intro" onclick="openPage(event, 'introduction')">Click here to begin!</a>
</div> -->
<div id="default" class="default-page">
<h3>Getting Started With</h3>
<h3 id="hidden-heading">JavaScript</h3>
<img src="images/js.png">
<br><br>
<p>powered by</p>
<a href="http://www.codingninjas.in/"><img src="images/logo.png"></a><br><br>
<a id="jump-to-intro" onclick="openPage(event, 'introduction')">Click here to begin!</a>
</div>
<div id="introduction" class="tabcontent">
<h3 class="heading">Introduction</h3>
<div class="content">
<!-- Start writing from here -->
<p class = "explanationText">
<b>JavaScript</b> is a light-weight, cross-platform, object-oriented language. Unlike C++ and Java, JavaScript is not a compiled language. It is an interpretted language like python, which means the JS code is executed line by line and if any error shows up, the interpretter stops there. JS is used for providing the control of HTML and CSS to the browser.
</p>
<p class="explanationText">
JS code can be interpretted on google-chrome or mozilla-firefox. For this tutorial, Google-chrome or Chromium-browser is preffered. Just press <kbd>ctrl+shit+i</kbd> and then go to <kbd>console</kbd> tab. You can either execute JS code line by line here or you can write the JS code in your favourite editor and then paste it here in the console. Now its time for the <em>hello-world</em> ritual. To print anything in the console, the syntax is:
</p>
<div class="code">
console.log("Hello, World!!");
</div>
<p class="explanationText">
Also checkout the syntax below. It is used to create alerts.
</p>
<div class="code">
alert("Message that you want to display");
</div>
<p class="explanationText">
By now you might have noticed <code><b>undefined</b></code> that gets printed on the console after every command. The console always prints the whatever the last command returns. The <code><b>print</b></code> and <code><b>alert</b></code> functions don't return anything, so, <code><b>undefined</b></code> is printed.
</p>
<h3 class="explanationHeading">
Comments in JS
</h3>
<p class="explanationText">
For in-line comments the following syntax is used:
</p>
<div class="code">
console.log("testing") //This is a comment!!<br>
//This whole line is a comment!
</div>
<p class="explanationText">
For multiple line comments:
</p>
<div class="code">
/*Multi-line comments are <br>
made in this way*/
</div>
<h3 class="explanationHeading">
Including javascript in a web page
</h3>
<p class="explanationText">
The tag <code><b>script</b></code> is used for including JS within a web page.
</p>
<xmp class="code"><script src="/path_to_file/file.js" type="text/javascript"></script></xmp>
<p class="explanationText">
For including the JS inside the HTML document, the JS code must be enclosed within the <code><b>script</b></code> tags and the <code><b>src</b></code> attribute should not be mentioned.
</p>
<div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
<div id="variables-and-data-types" class="tabcontent">
<h3 class="heading">Variables & Data Types</h3>
<div class="content">
<!-- Start writing from here -->
<h3 class="explanationHeading">Data types</h3>
<p class="explanationText">
There are two kind of data types:
<dl class="explanationList">
<dt>Primitive-types</dt>
<dd><em>Boolean</em>: a boolean data-type can have <code><b>true</b></code> or <code><b>false</b> values within it</code>.</dd>
<dd><em>Number</em>: It is used to store numeric data in JS.</dd>
<dd><em>String: Used to store sequence of characters to represent text.</em></dd>
<dd><em>Null: </em> it is used as a reference that points no-where!</dd>
<dd><em>Undefined: </em> to refer to something that does't exist.</dd>
<dt>Object</dt>
<dd>We will be studying about objects in a later section in this tutorial.</dd>
</dl>
</p>
<h3 class="explanationHeading">Variables</h3>
<p class="explanationText">
JS is a dynamic type language which means that you don't have to specify the type of variables. The type is automatically managed by the JS engine. Variables are defined using the keyword <code><b>var</b></code> as follows:
</p>
<div class="code">
var a = 42; //holding number <br>
var b = "Awesomenes" //holding string
</div>
<h3 class="explanationHeading">
Scope of Variables
</h3>
<p class="explanationText">
A variable can have two kind of scopes in JS:
<dl class="explanationText">
<dt>Global Variables</dt>
<dd>They are declared outside functions and can be accessed anywhere inside JS code.</dd>
<dt>Local Vaariables</dt>
<dd>They are declared inside functions and don't exist outside the functions. You will be reading about the JS functions in a later section.</dd>
</dl>
</p>
<div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
<div id="operators" class="tabcontent">
<h3 class="heading">Operators</h3>
<div class="content">
<!-- Start writing from here -->
<p class="explanationHeading">Arithmetic Operators</p>
<p class="explanationText">Javascript supports basic arithmetic operations like Addition <b>(+)</b>,subtraction <b>(-)</b>,multiplication <b>(*)</b>, division <b>(/)</b> and modulus <b>(%)</b>.</p>
<xmp class="code">console.log(13+12);
console.log(13-12);
console.log(5*3);
console.log(16/4);
console.log(15%4);
</xmp>
<p class="explanationText">on console..</p>
<xmp class="code">-> 25
-> 1
-> 15
-> 4
-> 3
</xmp>
<p class="explanationHeading">Assignment Operators</p>
<p class="explanationText">The Assignment operator <b>=</b> assigns a particular value to a variable.</p>
<xmp class="code">var x=3; //3 is assigned to x
</xmp>
<p class="explanationText"><b>Addition</b> assignment:</p>
<xmp class="code">var x = 10;
x +=5; //x=x+5;
console.log(x);
</xmp>
<p class="explanationText">on console..</p>
<xmp class="code">-> 15</xmp>
<p class="explanationText"><b>Multiplication</b> assignment:</p>
<xmp class="code">var y=10;
y*=5; //y=y*5;
console.log(y);
</xmp>
<p class="explanationText">on console..</p>
<xmp class="code">-> 50</xmp>
<p class="explanationText">In general..</p>
<xmp class="code">x = y (x = y) //Assignment
x += y (x = x + y) //Addition assignment
x -= y (x = x - y) //Subtraction assignment
x *= y (x = x * y) //Multiplication assignment
x /= y (x = x / y) //Division assignment
x %= y (x = x % y) //Remainder assignment
x **= y (x = x ** y) //Exponential assignment
x <<= y (x = x << y) //Left shift assignment
x >>= y (x = x >> y) //Right shift assignment
x >>>= y (x = x >>> y) //Unsigned right shift assignment
x &= y (x = x & y) //Bitwise AND assignment
x ^= y (x = x ^ y) //Bitwise XOR assignment
x |= y (x = x | y) //Bitwise OR assignment</xmp>
<p class="explanationHeading">Comparison Operators</p>
<p class="explanationText">The <b>Comparison </b>operator used to compare two values.</p>
<xmp class="code">if ('5'==5)
return true;
else
return false;</xmp>
<p class="explanationText">on console..</p>
<xmp class="code">-> true</xmp>
<p class="explanationText">But if we try <b>===</b> operator instead of <b>==</b> it will return <b>false</b> as the <b>===</b> operator strictly compares the two values.</p>
<xmp class="code">console.log(3>4); //-> false
console.log(3!=4); //-> true
console.log('5'!=5); //-> false
console.log('5'!==5); //-> true
</xmp>
<p class="explanationText">in general..</p>
<xmp class="code">== equal to
=== strictly equal to
!= not equal
!== strictly not equal
> greater than
< less than
>= greater than or equal to
<= less than or equal to</xmp>
<p class="explanationHeading">Bitwise Operators</p>
<p class="explanationText">A <b>bitwise</b> operation operates on one or more bit patterns or binary numerals at the level of their individual bits.</p>
<xmp class="code">console.log(1&0); //-> 0
console.log(1&1); //-> 1
console.log(!true); //-> false
</xmp>
<p class="explanationText">in general..</p>
<xmp class="code">& AND
~ NOT
^ XOR
<< shift left
>> shift right
>>> Zero-fill right shift</xmp>
<p class="explanationText">some more operations..</p>
<xmp class="code">console.log(7^3); //-> 4
console.log(9&1); //-> 1
console.log(5|2); //-> 7
</xmp>
<p class="explanationNote"><b>Note:</b>In case of <b>bitwise </b>operators the operands first converted to <b>binary </b>and then bitwise operations are performed b/w the converted binary numbers.</p>
<xmp class="code">console.log(7^3);
7 -> 0 1 1 1
3 -> ^ 0 0 1 1
-------------------
result-> 0 1 0 0 //-> 4
console.log(9&1);
9 -> 1 0 0 1
1 -> & 0 0 0 1
-------------------
result-> 0 0 0 1 //-> 1
console.log(5|2);
5 -> 0 1 0 1
2 -> 0 0 1 0
-------------------
result-> 0 1 1 1 //-> 7</xmp>
<p class="explanationHeading">String Operator</p>
<p class="explanationText">In javascript the <b>+ </b>operator is used to concatenate two strings.</p>
<xmp class="code">var x = "Robert";
var y = "Downey Jr";
console.log(x+" "+y); // -> "Robert Downey Jr"</xmp>
<xmp class="code">var smile = "LOL"
smile+=" :D"
console.log(smile); // -> "LOL :D"
</xmp>
<p class="explanationHeading">Logical Operators</p>
<p class="explanationText">Logical operators return true or false depending upon the value of operands.</p>
<p class="explanationText">There are three kinds of logical operators.</p>
<ul ><li class="explanationList"><b>&&</b> (logical AND)</li>
<li class="explanationList"><b>||</b> (logical OR)</li>
<li class="explanationList"><b>!</b> (logical NOT)</li></ul>
<xmp class="code">console.log(true&&true); // -> true
console.log(false&&true&&true); // -> false</xmp>
<p class="explanationNote"><b>Note:</b>While using <b>Logical AND</b> if any of the operand value is <b>false</b> then the resultant expression will return false else it will return <b>true</b>.</p>
<xmp class="code">console.log(true||true); // -> true
console.log(true||true||false); // -> true
console.log(false||false); // -> false</xmp>
<p class="explanationNote"><b>Note:</b> In case of <b>Logical OR </b>if any of the operand value is <b>true</b> then the expression also returns true else it will return <b>false</b>.</p>
<xmp class="code">console.log(!true); // -> false
console.log(!false); // -> true</xmp>
<p class="explanationHeading">Conditional Operator</p>
<xmp class="code">console.log(4<3?first:second); // -> second
console.log(4>3?first:second); // -> first</xmp>
<p class="explanationText">In general..</p>
<xmp class="code">condition ? first : second</xmp>
<p class="explanationText">Conditional operator also known as <b>ternary</b> operator returns <b>first</b> if the condition is true else returns <b>second</b> if the condition is false.</p>
<div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
<!--<div id="logical-operators" class="tabcontent">
<h3 class="heading">Logical Operators</h3>
<div class="content">
<!-- Start writing from here -->
<!-- <div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
<div id="conditional-operators" class="tabcontent">
<h3 class="heading">Conditional Operators</h3>
<div class="content">
<!-- Start writing from here -->
<!-- <div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>-->
<div id="for-while" class="tabcontent">
<h3 class="heading" id="linksHeading">For, While</h3>
<div class="content">
<!-- Start writing from here -->
<p class="explanationText">For performing an action again and again loops are used in javascript.</p>
<p class="explanationHeading">The "for" loop.</p>
<xmp class="code">for(initialisation;terminationCondition;increment/decrement)
{
// action
}</xmp>
<p class="explanationText">The first value is the <b>initialisation</b> condition(from where loop starts),the second one is the <b>termination</b> condition(where loop ends) and the third one is the <b>increment/decrement</b> condition(the value incremented or decremented by the provided value).</p>
<xmp class="code">for(var i=0;i<5;i++)
{
console.log("for loop prints: "+i);
}</xmp>
<p class="explanationText">on console..</p>
<xmp class="code">"for loop prints: 0"
"for loop prints: 1"
"for loop prints: 2"
"for loop prints: 3"
"for loop prints: 4"</xmp>
<p class="explanationText">nested "for" loops..</p>
<xmp class="code">for (var i = 1; i <= 5; i++) {
var output = "";
for (var j = 1; j <= i; j++) {
output += "*"
}
console.log(output);
}</xmp>
<p class="explanationText">on console..</p>
<xmp class="code">"*"
"**"
"***"
"****"
"*****"</xmp>
<p class="explanationHeading">The "for-in" loop</p>
<p class="explanationText">The <b>for-in </b>loop iterates through the properties of an <b>object</b>.</p>
<xmp class="code">var iceCream = {name:"Vadilal", flavour:"ButterScotch", type:"Cornetto"};
var taste = "";
var x;
for (x in iceCream) {
taste += iceCream[x]+ " ";
}
console.log(taste);</xmp>
<p class="explanationText">on console..</p>
<xmp class="code">"Vadilal ButterScotch Cornetto "</xmp>
<p class="explanationText"><b>Vadilal ButterScotch Cornetto</b> are the properties of the <b>iceCream </b>object.</p>
<p class="explanationText">Javascript <b>objects </b>will be discussed in the later section.</p>
<p class="explanationHeading">The "While" loop</p>
<p class="explanationText">The <b>while</b> loop works until a condition returns false.</p>
<xmp class="code">var i=5; //initialisation
while(i>=1) //termination condition
{
console.log("While loop prints: "+i);
i--; //decrement condition
}</xmp>
<p class="explanationText">on console..</p>
<xmp class="code">"While loop prints: 5"
"While loop prints: 4"
"While loop prints: 3"
"While loop prints: 2"
"While loop prints: 1"</xmp>
<p class="explanationHeading">The "Do-while" loop</p>
<xmp class="code">var test=" ";
var i=0;
do {
test += "Do-While prints " + i+'\n';
i++;
}
while (i < 5);
console.log(test);</xmp>
<p class="explanationText">on console..</p>
<xmp class="code">" Do-While prints 0
Do-While prints 1
Do-While prints 2
Do-While prints 3
Do-While prints 4
"</xmp>
<p class="explanationText">in general..</p>
<xmp class="code">do
statement
while (condition);
</xmp>
<div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
<div id="functions" class="tabcontent">
<h3 class="heading">Functions</h3>
<div class="content">
<!-- Start writing from here -->
<p class="explanationText">For printing something on console..</p>
<xmp class="code">console.log("Can't talk what's app only!!");</xmp>
<p class="explanationText">Well, that's one way to go another way is to use <b>functions</b>.</p>
<xmp class="code">function print(text){
console.log(text);
}</xmp>
<xmp class = "code">print("Can't talk what's app only!!");</xmp>
<p class="explanationText">Will do the same.</p>
<p class="explanationText">A function in javascript can be used by using the keyword <b>function</b> followed by the <b>functionName</b>.Inside this function the whole block of code can be written.This is called defining the function.</p>
<p class="explanationText">In order to use that function we have to <b>call</b> or <b>invoke</b> that function, this can be done by writing the name of the function followed by <b>( )</b>.</p>
<p class="explanationText">A function can be passed with or without <b>arguments</b>.These <b>arguments</b> can be one or more than one.</p>
<xmp class ="code">function noArgument(){
console.log("Hello-Kitty!!");
}
noArgument(); //-> "Hello-Kitty!!"</xmp>
<xmp class="code">function withArguments(one,two){
console.log("My name is " +one+ " and i luv " +two+ " :-P");
}
withArguments("Kamal","Pizza");
//-> "My name is Kamal and i luv Pizza :-P"</xmp>
<p class="explanationText">In the <b>withArgument(one, two)</b> function the <b>one</b> and <b>two</b> are the arguments that are therefore used in the console.log() function.</p>
<p class="explanationText">in general..</p>
<xmp class="code">function functionName(argument1,argument2,----){
//set of instructions..
}</xmp>
<p class="explanationText">To send a value back out of a function we can use a <b>return</b> statement.</p>
<xmp class="code">function valueBack(a, b)
{
return a+b;
}
var x = valueBack(311,567);
console.log(x); //-> 878
</xmp>
<p class="explanationText">In order to use this returned value we have to call the <b>valueBack(311,567)</b> function and then assign this to a variable <b>x</b>.</p>
<p class="explanationNote"><b>Note:</b> Any last statement in the javascript function will be treated as the return statement.</p>
<p class="explanationText">Any variable that is defined outside the function has a <b>global scope</b> and can be used outside as well as the inside of a function.</p>
<p class="explanationText">Any variable that is defined inside of a function is of <b>local scope</b> and can be used inside of the function only.</p>
<p class="explanationNote"><b>Note: </b>The function arguments are also considered in the local scope.</p>
<xmp class="code">var x = 21; //global scope
function fun(){
var y = 22; //local scope
console.log("I am "+ x +", next year I'm gonna be "+y);
}
fun(); //-> "I am 21, next year I'm gonna be 22"
</xmp>
<div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
<div id="array" class="tabcontent">
<h3 class="heading">Array</h3>
<div class="content">
<!-- Start writing from here -->
<p class="explanationText">Arrays are used to store <b>multiple</b> values in a single variable.</p>
<xmp class="code">var comedy = ["F.R.I.E.N.D.S","The Big Bang Theory",
"Two And a Half Men","How i Met Your Mother"];</xmp>
<p class="explanationText">In the above code the different values are stored in a single variable named <b>comedy</b>.</p>
<p class="explanationText">The first element is located at the position <b>0</b> the second at the position <b>1</b> and so on..</p>
<xmp class="code">console.log(comedy[0]) //-> "F.R.I.E.N.D.S";
console.log(comedy[2]); //-> "Two And a Half Men"</xmp>
<p class="explanationText">We can also iterate through the whole array using the <b>for</b> loop.</p>
<xmp class = "code">for(var i=0;i< comedy.length;i++)
{
console.log(comedy[i]);
}</xmp>
<p class="explanationText">results in..</p>
<xmp class="code">"F.R.I.E.N.D.S"
"The Big Bang Theory"
"Two And a Half Men"
"How i Met Your Mother"</xmp>
<p class="explanationText">Also, multiple <b>datatypes</b> can be added in an array.<p>
<xmp class="code">var imdb = ["F.R.I.E.N.D.S.",9,"How i Met Your Mother",8.4];</xmp>
<xmp class="code">console.log(imdb[3]); //-> 8.4</xmp>
<p class="explanationText">These are called <b>1-D</b> arrays.</p>
<p class="explanationText">We can also add multiple arrays inside an array using <b>multi-dimentional</b> arrays.</p>
<xmp class="code">var cast = [["Sheldon", "Penny", "Leonard" , "Howard", "Raj"],
["Ted", "Robin", "Lily", "Marshal", "Barney"]];</xmp>
<p class="explanationText">There are two arrays inside an array naming <b>cast</b> each one of these can be accessed by using two square brackets.</p>
<xmp class="code">var x = cast[1][4];
console.log(x); //-> "Barney"</xmp>
<xmp class="code">console.log(cast[0][0]); //-> "Sheldon"</xmp>
<p class="explanationText">It is also possible to have multiple nested arrays as many as possible.</p>
<xmp class="code">var num = [
[1,2,3],
[[4,5,6], 7, 8],
[9,10,11],
[12,13,14]
];</xmp>
<xmp class="code">var x = num[1][0][1];
console.log(x); //-> 5
</xmp>
<p class="explanationHeading">Methods in an array</p>
<!--<li class="explanationList">push() and pop()</li>
<li class="explanationList">shift() and unshift()</li>
<li class="explanationList"></li>
<li class="explanationList"></li>
-->
<li class="explanationList">The <b>push()</b> method is used to append a data to the end of an array.</li>
<xmp class="code">var x = ['a','b','c'];
x.push('d');
console.log(x); //-> ["a", "b", "c", "d"]</xmp>
<xmp class="code">var myArray = [["Charlie", 42, "Men"], ["Jake", 15, "Boy"]];
myArray.push(["Alan", 40, "Men"]);
console.log(myArray); //-> [["Charlie", 42, "Men"], ["Jake", 15, "Boy"], ["Alan", 40, "Men"]]</xmp>
<li class="explanationList">The <b>pop()</b> method removes an element from the end of an array.</li>
<xmp class="code">var myArray = [["Charlie", 42, "Men"], ["Jake", 15, "Boy"]];
myArray.pop();
console.log(myArray); //-> [["Charlie", 42, "Men"]]</xmp>
<li class="explanationList">The <b>shift()</b> method removes an element from the begining of an array.</li>
<xmp class="code">var myArray = [["Charlie", 42, "Men"], ["Jake", 15, "Boy"]];
myArray.shift();
console.log(myArray); //-> [["Jake", 15, "Boy"]]</xmp>
<li class="explanationList">The <b>unshift() </b>method adds an element at the starting index of an array.</li>
<xmp class="code">var myArray = [["Charlie", 42, "Men"], ["Jake", 15, "Boy"]];
myArray.unshift(["Alan", 40, "Men"]);
console.log(myArray); //-> [["Alan", 40, "Men"], ["Charlie", 42, "Men"], ["Jake", 15, "Boy"]]</xmp>
<li class="explanationList">The <b>reverse() </b>method reverses the array.</li>
<xmp class="code">var x = ["one","two","three","four","five"];
x.reverse();
console.log(x); //-> ["five", "four", "three", "two", "one"]</xmp>
<li class="explanationList">The <b>sort() </b>method sorts an array.</li>
<xmp class="code">var friends = ["Rachel", "Chandler", "Ross", "Monica", "Joey", "Phoebe"];
friends.sort();
console.log(friends); //-> ["Chandler", "Joey", "Monica","Phoebe", "Rachel", "Ross"]</xmp>
<xmp class ="code">var num = [35,150,75];
console.log(num.sort()); //-> [150, 35, 75]</xmp>
<p class="explanationText">Which is incorrect!.This happens because the <b>sort()</b> method sorts the array values in an alphabetical or in ascending order by converting the values into strings, hence the "1" in "150" comes before the "3" in "35" and the "7" of "75" comes after "3" of "35" hence results in [150, 35, 75].</p>
<p class="explanationText">In order to avoid this a compare() function should be passed with the <b>sort()</b> method.</p>
<xmp class="code">num.sort(function(a, b) {
return a - b;
});
console.log(num); //-> [35,75,150]</xmp>
<p class="explanationText">The compare function returns a negative number if a is before b, a positive number if a is after b, or 0 if they are equal.</p>
<p class="explanationText">The above array can also be sorted in decreasing order just by changing the order of a with b and b with a.</p>
<xmp class="code">num.sort(function(a, b) {
return b - a;
});
console.log(num); //-> [150,75,35]</xmp>
<p class="explanationText">Returns the array in decreasing order.</p>
<li class="explanationList">The <b>concat()</b> method joins two arrays.</li>
<xmp class="code">var boys = ["Chandler","Ross", "Joey"];
var girls = ["Monica", "Rachel", "Phoebe"];
var friends = boys.concat(girls);
console.log(friends); //-> ["Chandler", "Ross", "Joey", "Monica", "Rachel", "Phoebe"]</xmp>
<li class="explanationList">The <b>slice()</b> takes a particular part from an array.</li>
<xmp class="code">var x = [34,23,88,3,10];
console.log(x.slice(2)); //-> [88,3,10]
console.log(x.slice(1,4)); //-> [23,88,3]</xmp>
<p class="explanationNote"><b>Note:</b>The <b>slice(2)</b> method returns rest of the values starting from index 2, whereas the <b>slice(1,4)</b> returns the elements present from index 1 to 3 excluding the element present at the fourth index.</p>
<div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
<div id="closures" class="tabcontent">
<h3 class="heading">Closures</h3>
<div class="content">
<!-- Start writing from here -->
<p class="explanationNote"><em>NOTE: </em> Read this page very very carefully. The topic covered is the heart of Object Oriented Programming using JS.</p>
<p class="explanationText">
The local variables inside the funtions are recreated each time a function is called and these are independent of each other. Normally, in other languages when any function exits, the local variables are destroyed. JS has the ability to store functions as variables under some circumstances. Since functions can be stored in variable names like:
</p>
<xmp class="code">var fun = function(){
//function Code
}</xmp>
<div class="explanationText">
A variable containing a function can be returned from another function. Now the returned function can be stored in another variable and the returned function can be executed later on as follows:
</div>
<xmp class="code">function outerFunction(){
var innerFunction = function(){
console.log("inner function was executed");
}
return innerFunction;
}
var fun = outerFunction();
fun();</xmp>
<p class="explanationText">
Now consider the case below in which the returned function contains the local variables of the function being called.
</p>
<xmp class="code">function outerFunction1(num){
var innerVar = 10*num;
return function(){
console.log(innerVar);
}
}
var fun1 = outerFunction1(1);
var fun2 = outerFunction1(2);
var fun3 = outerFunction1(3);
var fun4 = outerFunction1(4);
fun1();
fun2();
fun3();
fun4();
fun1();</xmp>
<p class="explanationText">
The above example prints the following things on the console.
</p>
<xmp class="code">>10
>20
>30
>40
>10</xmp>
<p class="explanationText">
You might think why this is happening. The answer is that the local variables of the binding function are being used in the function that is being returned, then JS interpretter is smart enough to not to destroy the local variables. And since on each new call to any function the local variables are recreated, the returned functions have independent variables i.e, changing value of local variables of any of the returned function won't effect the others. <br>
This feature of storing specific intance of local variables of enclosing function is called <em>closure</em>.
A <em>closure</em> might be thought of as a closed packet of local variables of the enclosing function, that are being used in the function to be returned.
</p>
<p class="explanationNote">
<em>NOTE:</em> It is very important to use the keyword <code><b>var</b> before the variables that will be used in the closure because if the variable with same name exists in global scope too, closure won't be formed as desired and the global scope variable would be used in the function!</code>
</p>
<div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
<div id="objects" class="tabcontent">
<h3 class="heading">Objects</h3>
<div class="content">
<!-- Start writing from here -->
<p class="explanationText">
Concepts of Object Oriented Programming in JS are implemented a bit different manner from languages like, Java or C++. In Java Objects are a collection of properties which are stored in key and value pairs. Now you might wonder how the objects would have functions? This is simple, the ability of JS to store functions as the variables is exploited here! So, in JS, the objects are stored as a collection of key-value pairs. If all this seems fuzzy just wait for all the explanation below.
</p>
<h4 class="explanationHeading">Ways to access object properties</h4>
<p class="explanationText">
To access the object properties, we use the following syntax:
<xmp class="code">objectName.propertyName;</xmp>
</p>
<p class="explanationText">
This syntax is also valid:
</p>
<xmp class="code">object["propertyName"];</xmp>
<p class="explanationText">
In the above usage, first the expression inside the square brackets is evaluated and then then the final string formed is matched with the properties of the object.
<br>
In the case when the called property of the object doesn't exist, <code><b>undefined</b></code> is returned.
</p>
<h4 class="explanationHeading">Declaring an Object</h4>
<p class="explanationText">
There are many ways of declaring objects in JS each one with its own benefits.
</p>
<p class="explanationText">The following syntax is used for declaring an Object and for declaring object properties:</p>
<xmp class="code">var myPC = new Object();
myPC.make = "hp";
myPC.model = "pavillion";
myPC.year = 2014;</xmp>
<h2 class="explanationSubHeading">
Using object intiializers
</h2>
<p class="explanationText">
The syntax for an object using an object initializer is:
</p>
<xmp class="code">var obj = { property_1: value_1, 2:value_2,
// ...,"property n": value_n }; </xmp>
<p class="explanationText">
The <code>property</code> can be of type String or integer and the <code>value</code> can store anything like a <code>var</code>!
</p>
<h5 class="explanationSubHeading">
Using constructor
</h5>
<p class="explanationText">
For this form, first a constructor function is made and to make an object <code><b>new</b></code> keyword is used!
</p>
<xmp class="code">function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var myCar = new Car("Toyota","Fortuner",2017)</xmp>
<p class="explanationText">
When <code><b>new</b></code> keyword is used a new space is allocated for the variable and in the constructor the object is refered using <code><b>this</b></code> keyword.
</p>
<h5 class="explanationSubHeading">Using <code>Object.create</code> method</h5>
<p class="explanationText">
Objects can also be created using the Object.create() method. This method can be very useful, because it allows you to choose the prototype object for the object you want to create, without having to define a constructor function. You will be studying about prototypes in the next section! The code is:
</p>
<xmp class="code">// Animal properties and method encapsulation
var Animal = {
type: "Invertebrates", // Default value of properties
displayType : function() { // Method which will display type of Animal
console.log(this.type);
}
}
// Create new animal type called animal1
var animal1 = Object.create(Animal);
animal1.displayType(); // Output:Invertebrates
// Create new animal type called Fishes
var fish = Object.create(Animal);
fish.type = "Fishes";
fish.displayType(); // Output:Fishes</xmp>
<h4 class="explanationHeading">Iterating object properties</h4>
<p class="explanationText">
The object properties can be iterated using the <code><b>for...in</b></code> loop construct as follows:
</p>
<xmp class="code">var obj = myPC;
for (var prop in obj) {
console.log('obj.' + prop, '=', obj[prop]);
}</xmp>
<div class=""></div>
<div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
<div id="prototypes" class="tabcontent">
<h3 class="heading">Prototypes</h3>
<div class="content">
<!-- Start writing from here -->
<div class="navigationArrows">
<img src="images/arrow_left.svg" title="Previous section" onclick="previousPage()">
<img src="images/arrow_right.svg" title="Next Section" onclick="nextPage()">
</div>
</div>
</div>
</div>
</body>
</html>