-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.html
841 lines (762 loc) · 102 KB
/
javascript.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
<!DOCTYPE html>
<html lang="en" xml:lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>D JavaScript | Introduction to Programming</title>
<meta name="description" content="Course reader for IMT/LIS 511: Introduction to Programming for Information and Data Science." />
<meta name="generator" content="bookdown 0.24 and GitBook 2.6.7" />
<meta property="og:title" content="D JavaScript | Introduction to Programming" />
<meta property="og:type" content="book" />
<meta property="og:url" content="https://infx511.github.io/" />
<meta property="og:description" content="Course reader for IMT/LIS 511: Introduction to Programming for Information and Data Science." />
<meta name="github-repo" content="infx511/book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="D JavaScript | Introduction to Programming" />
<meta name="twitter:description" content="Course reader for IMT/LIS 511: Introduction to Programming for Information and Data Science." />
<meta name="author" content="Joel Ross" />
<meta name="date" content="2023-07-26" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="shortcut icon" href="img/ischool-gold.png" type="image/x-icon" />
<link rel="prev" href="command-line.html"/>
<script src="libs/header-attrs-2.11/header-attrs.js"></script>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.0.1/anchor-sections.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.0.1/anchor-sections.js"></script>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/prism.min.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./" class="title">Introduction to Programming</a></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>About this Book</a></li>
<li class="part"><span><b>I Python Basics</b></span></li>
<li class="chapter" data-level="1" data-path="python-intro.html"><a href="python-intro.html"><i class="fa fa-check"></i><b>1</b> Introduction to Python</a>
<ul>
<li class="chapter" data-level="1.1" data-path="python-intro.html"><a href="python-intro.html#programming-with-python"><i class="fa fa-check"></i><b>1.1</b> Programming with Python</a>
<ul>
<li class="chapter" data-level="1.1.1" data-path="python-intro.html"><a href="python-intro.html#versions"><i class="fa fa-check"></i><b>1.1.1</b> Versions</a></li>
</ul></li>
<li class="chapter" data-level="1.2" data-path="python-intro.html"><a href="python-intro.html#running-python-code"><i class="fa fa-check"></i><b>1.2</b> Running Python Code</a>
<ul>
<li class="chapter" data-level="1.2.1" data-path="python-intro.html"><a href="python-intro.html#jupyter-notebooks"><i class="fa fa-check"></i><b>1.2.1</b> Jupyter Notebooks</a></li>
<li class="chapter" data-level="1.2.2" data-path="python-intro.html"><a href="python-intro.html#on-the-command-line"><i class="fa fa-check"></i><b>1.2.2</b> On the Command Line</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="python-intro.html"><a href="python-intro.html#comments"><i class="fa fa-check"></i><b>1.3</b> Comments</a></li>
<li class="chapter" data-level="1.4" data-path="python-intro.html"><a href="python-intro.html#variables"><i class="fa fa-check"></i><b>1.4</b> Variables</a>
<ul>
<li class="chapter" data-level="1.4.1" data-path="python-intro.html"><a href="python-intro.html#data-types"><i class="fa fa-check"></i><b>1.4.1</b> Data Types</a></li>
</ul></li>
<li class="chapter" data-level="1.5" data-path="python-intro.html"><a href="python-intro.html#getting-help"><i class="fa fa-check"></i><b>1.5</b> Getting Help</a></li>
<li class="chapter" data-level="" data-path="python-intro.html"><a href="python-intro.html#resources"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="functions.html"><a href="functions.html"><i class="fa fa-check"></i><b>2</b> Functions</a>
<ul>
<li class="chapter" data-level="2.1" data-path="functions.html"><a href="functions.html#what-are-functions"><i class="fa fa-check"></i><b>2.1</b> What are Functions?</a></li>
<li class="chapter" data-level="2.2" data-path="functions.html"><a href="functions.html#python-function-syntax"><i class="fa fa-check"></i><b>2.2</b> Python Function Syntax</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="functions.html"><a href="functions.html#object-methods"><i class="fa fa-check"></i><b>2.2.1</b> Object Methods</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="functions.html"><a href="functions.html#built-in-python-functions"><i class="fa fa-check"></i><b>2.3</b> Built-in Python Functions</a>
<ul>
<li class="chapter" data-level="2.3.1" data-path="functions.html"><a href="functions.html#modules-and-libraries"><i class="fa fa-check"></i><b>2.3.1</b> Modules and Libraries</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="functions.html"><a href="functions.html#writing-functions"><i class="fa fa-check"></i><b>2.4</b> Writing Functions</a>
<ul>
<li class="chapter" data-level="2.4.1" data-path="functions.html"><a href="functions.html#doc-strings"><i class="fa fa-check"></i><b>2.4.1</b> Doc Strings</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="functions.html"><a href="functions.html#resources-1"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="logic.html"><a href="logic.html"><i class="fa fa-check"></i><b>3</b> Logic and Conditionals</a>
<ul>
<li class="chapter" data-level="3.1" data-path="logic.html"><a href="logic.html#booleans"><i class="fa fa-check"></i><b>3.1</b> Booleans</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="logic.html"><a href="logic.html#relational-operators"><i class="fa fa-check"></i><b>3.1.1</b> Relational Operators</a></li>
<li class="chapter" data-level="3.1.2" data-path="logic.html"><a href="logic.html#boolean-operators"><i class="fa fa-check"></i><b>3.1.2</b> Boolean Operators</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="logic.html"><a href="logic.html#conditional-statements"><i class="fa fa-check"></i><b>3.2</b> Conditional Statements</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="logic.html"><a href="logic.html#designing-conditions"><i class="fa fa-check"></i><b>3.2.1</b> Designing Conditions</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="logic.html"><a href="logic.html#determining-module-or-script"><i class="fa fa-check"></i><b>3.3</b> Determining Module or Script</a></li>
<li class="chapter" data-level="" data-path="logic.html"><a href="logic.html#resources-2"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="lists.html"><a href="lists.html"><i class="fa fa-check"></i><b>4</b> Lists and Sequences</a>
<ul>
<li class="chapter" data-level="4.1" data-path="lists.html"><a href="lists.html#what-is-a-list"><i class="fa fa-check"></i><b>4.1</b> What is a List?</a></li>
<li class="chapter" data-level="4.2" data-path="lists.html"><a href="lists.html#list-indices"><i class="fa fa-check"></i><b>4.2</b> List Indices</a></li>
<li class="chapter" data-level="4.3" data-path="lists.html"><a href="lists.html#list-operations-and-methods"><i class="fa fa-check"></i><b>4.3</b> List Operations and Methods</a></li>
<li class="chapter" data-level="4.4" data-path="lists.html"><a href="lists.html#nested-lists"><i class="fa fa-check"></i><b>4.4</b> Nested Lists</a></li>
<li class="chapter" data-level="4.5" data-path="lists.html"><a href="lists.html#other-sequences"><i class="fa fa-check"></i><b>4.5</b> Other Sequences</a>
<ul>
<li class="chapter" data-level="4.5.1" data-path="lists.html"><a href="lists.html#ranges"><i class="fa fa-check"></i><b>4.5.1</b> Ranges</a></li>
<li class="chapter" data-level="4.5.2" data-path="lists.html"><a href="lists.html#tuples"><i class="fa fa-check"></i><b>4.5.2</b> Tuples</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="lists.html"><a href="lists.html#resources-3"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="loops.html"><a href="loops.html"><i class="fa fa-check"></i><b>5</b> Iterating with Loops</a>
<ul>
<li class="chapter" data-level="5.1" data-path="loops.html"><a href="loops.html#for-loops"><i class="fa fa-check"></i><b>5.1</b> For Loops</a>
<ul>
<li class="chapter" data-level="5.1.1" data-path="loops.html"><a href="loops.html#variables-and-loops"><i class="fa fa-check"></i><b>5.1.1</b> Variables and Loops</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="loops.html"><a href="loops.html#lists-and-loops"><i class="fa fa-check"></i><b>5.2</b> Lists and Loops</a></li>
<li class="chapter" data-level="5.3" data-path="loops.html"><a href="loops.html#nested-loops"><i class="fa fa-check"></i><b>5.3</b> Nested Loops</a></li>
<li class="chapter" data-level="5.4" data-path="loops.html"><a href="loops.html#list-comprehensions"><i class="fa fa-check"></i><b>5.4</b> List Comprehensions</a></li>
<li class="chapter" data-level="5.5" data-path="loops.html"><a href="loops.html#while-loops"><i class="fa fa-check"></i><b>5.5</b> While Loops</a>
<ul>
<li class="chapter" data-level="5.5.1" data-path="loops.html"><a href="loops.html#counting-with-while-loops"><i class="fa fa-check"></i><b>5.5.1</b> Counting with While Loops</a></li>
<li class="chapter" data-level="5.5.2" data-path="loops.html"><a href="loops.html#sentinels"><i class="fa fa-check"></i><b>5.5.2</b> Sentinels</a></li>
<li class="chapter" data-level="5.5.3" data-path="loops.html"><a href="loops.html#difference-between-for-and-while-loops"><i class="fa fa-check"></i><b>5.5.3</b> Difference Between For and While Loops</a></li>
</ul></li>
<li class="chapter" data-level="5.6" data-path="loops.html"><a href="loops.html#iterating-over-files"><i class="fa fa-check"></i><b>5.6</b> Iterating over Files</a>
<ul>
<li class="chapter" data-level="5.6.1" data-path="loops.html"><a href="loops.html#tryexcept"><i class="fa fa-check"></i><b>5.6.1</b> Try/Except</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="loops.html"><a href="loops.html#resources-4"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="dictionaries.html"><a href="dictionaries.html"><i class="fa fa-check"></i><b>6</b> Dictionaries</a>
<ul>
<li class="chapter" data-level="6.1" data-path="dictionaries.html"><a href="dictionaries.html#what-is-a-dictionary"><i class="fa fa-check"></i><b>6.1</b> What is a Dictionary?</a></li>
<li class="chapter" data-level="6.2" data-path="dictionaries.html"><a href="dictionaries.html#accessing-a-dictionary"><i class="fa fa-check"></i><b>6.2</b> Accessing a Dictionary</a></li>
<li class="chapter" data-level="6.3" data-path="dictionaries.html"><a href="dictionaries.html#dictionary-methods"><i class="fa fa-check"></i><b>6.3</b> Dictionary Methods</a></li>
<li class="chapter" data-level="6.4" data-path="dictionaries.html"><a href="dictionaries.html#nesting-dictionaries"><i class="fa fa-check"></i><b>6.4</b> Nesting Dictionaries</a></li>
<li class="chapter" data-level="6.5" data-path="dictionaries.html"><a href="dictionaries.html#dictionaries-and-loops"><i class="fa fa-check"></i><b>6.5</b> Dictionaries and Loops</a>
<ul>
<li class="chapter" data-level="6.5.1" data-path="dictionaries.html"><a href="dictionaries.html#dictionary-comprehensions"><i class="fa fa-check"></i><b>6.5.1</b> Dictionary Comprehensions</a></li>
</ul></li>
<li class="chapter" data-level="6.6" data-path="dictionaries.html"><a href="dictionaries.html#which-data-structure-do-i-use"><i class="fa fa-check"></i><b>6.6</b> Which Data Structure Do I Use?</a></li>
<li class="chapter" data-level="" data-path="dictionaries.html"><a href="dictionaries.html#resources-5"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="searching.html"><a href="searching.html"><i class="fa fa-check"></i><b>7</b> Searching and Filtering</a>
<ul>
<li class="chapter" data-level="7.1" data-path="searching.html"><a href="searching.html#linear-search"><i class="fa fa-check"></i><b>7.1</b> Linear Search</a>
<ul>
<li class="chapter" data-level="7.1.1" data-path="searching.html"><a href="searching.html#maximal-search"><i class="fa fa-check"></i><b>7.1.1</b> Maximal Search</a></li>
<li class="chapter" data-level="7.1.2" data-path="searching.html"><a href="searching.html#falsification-search"><i class="fa fa-check"></i><b>7.1.2</b> Falsification Search</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="searching.html"><a href="searching.html#filtering"><i class="fa fa-check"></i><b>7.2</b> Filtering</a></li>
<li class="chapter" data-level="7.3" data-path="searching.html"><a href="searching.html#mapping"><i class="fa fa-check"></i><b>7.3</b> Mapping</a></li>
<li class="chapter" data-level="7.4" data-path="searching.html"><a href="searching.html#search-efficiency"><i class="fa fa-check"></i><b>7.4</b> Search Efficiency</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="searching.html"><a href="searching.html#linear-search-speed"><i class="fa fa-check"></i><b>7.4.1</b> Linear Search Speed</a></li>
<li class="chapter" data-level="7.4.2" data-path="searching.html"><a href="searching.html#faster-searching-binary-search"><i class="fa fa-check"></i><b>7.4.2</b> Faster Searching: Binary Search</a></li>
<li class="chapter" data-level="7.4.3" data-path="searching.html"><a href="searching.html#slower-algorithms-sorting"><i class="fa fa-check"></i><b>7.4.3</b> Slower Algorithms: Sorting</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="searching.html"><a href="searching.html#resources-6"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="functional-programming.html"><a href="functional-programming.html"><i class="fa fa-check"></i><b>8</b> Functional Programming</a>
<ul>
<li class="chapter" data-level="8.1" data-path="functional-programming.html"><a href="functional-programming.html#functions-are-variables"><i class="fa fa-check"></i><b>8.1</b> Functions ARE Variables</a>
<ul>
<li class="chapter" data-level="8.1.1" data-path="functional-programming.html"><a href="functional-programming.html#lambdas-anonymous-functions"><i class="fa fa-check"></i><b>8.1.1</b> lambdas: Anonymous Functions</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="functional-programming.html"><a href="functional-programming.html#functional-looping"><i class="fa fa-check"></i><b>8.2</b> Functional Looping</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="functional-programming.html"><a href="functional-programming.html#map"><i class="fa fa-check"></i><b>8.2.1</b> Map</a></li>
<li class="chapter" data-level="8.2.2" data-path="functional-programming.html"><a href="functional-programming.html#filter"><i class="fa fa-check"></i><b>8.2.2</b> Filter</a></li>
<li class="chapter" data-level="8.2.3" data-path="functional-programming.html"><a href="functional-programming.html#reduce"><i class="fa fa-check"></i><b>8.2.3</b> Reduce</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="functional-programming.html"><a href="functional-programming.html#resources-7"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="part"><span><b>II Specific Libraries</b></span></li>
<li class="chapter" data-level="9" data-path="pandas.html"><a href="pandas.html"><i class="fa fa-check"></i><b>9</b> The <code>pandas</code> Library</a>
<ul>
<li class="chapter" data-level="9.1" data-path="pandas.html"><a href="pandas.html#setting-up-pandas"><i class="fa fa-check"></i><b>9.1</b> Setting up <code>pandas</code></a></li>
<li class="chapter" data-level="9.2" data-path="pandas.html"><a href="pandas.html#series"><i class="fa fa-check"></i><b>9.2</b> Series</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="pandas.html"><a href="pandas.html#series-operations"><i class="fa fa-check"></i><b>9.2.1</b> Series Operations</a></li>
<li class="chapter" data-level="9.2.2" data-path="pandas.html"><a href="pandas.html#series-methods"><i class="fa fa-check"></i><b>9.2.2</b> Series Methods</a></li>
<li class="chapter" data-level="9.2.3" data-path="pandas.html"><a href="pandas.html#series-indexing"><i class="fa fa-check"></i><b>9.2.3</b> Series Indexing</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="pandas.html"><a href="pandas.html#dataframes"><i class="fa fa-check"></i><b>9.3</b> DataFrames</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="pandas.html"><a href="pandas.html#dataframe-operations"><i class="fa fa-check"></i><b>9.3.1</b> DataFrame Operations</a></li>
<li class="chapter" data-level="9.3.2" data-path="pandas.html"><a href="pandas.html#dataframe-methods"><i class="fa fa-check"></i><b>9.3.2</b> DataFrame Methods</a></li>
<li class="chapter" data-level="9.3.3" data-path="pandas.html"><a href="pandas.html#accessing-dataframes"><i class="fa fa-check"></i><b>9.3.3</b> Accessing DataFrames</a></li>
</ul></li>
<li class="chapter" data-level="9.4" data-path="pandas.html"><a href="pandas.html#grouping"><i class="fa fa-check"></i><b>9.4</b> Grouping</a>
<ul>
<li class="chapter" data-level="9.4.1" data-path="pandas.html"><a href="pandas.html#aggregation"><i class="fa fa-check"></i><b>9.4.1</b> Aggregation</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="pandas.html"><a href="pandas.html#resources-8"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html"><i class="fa fa-check"></i><b>10</b> Accessing Web APIs</a>
<ul>
<li class="chapter" data-level="10.1" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#web-apis"><i class="fa fa-check"></i><b>10.1</b> Web APIs</a></li>
<li class="chapter" data-level="10.2" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#restful-requests"><i class="fa fa-check"></i><b>10.2</b> RESTful Requests</a>
<ul>
<li class="chapter" data-level="10.2.1" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#uris"><i class="fa fa-check"></i><b>10.2.1</b> URIs</a></li>
<li class="chapter" data-level="10.2.2" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#http-verbs"><i class="fa fa-check"></i><b>10.2.2</b> HTTP Verbs</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#accessing-web-apis-1"><i class="fa fa-check"></i><b>10.3</b> Accessing Web APIs</a></li>
<li class="chapter" data-level="10.4" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#json-data"><i class="fa fa-check"></i><b>10.4</b> JSON Data</a></li>
<li class="chapter" data-level="" data-path="accessing-web-apis.html"><a href="accessing-web-apis.html#resources-9"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="appendix"><span><b>Appendix</b></span></li>
<li class="chapter" data-level="A" data-path="installing.html"><a href="installing.html"><i class="fa fa-check"></i><b>A</b> Installing Python</a>
<ul>
<li class="chapter" data-level="A.1" data-path="installing.html"><a href="installing.html#python"><i class="fa fa-check"></i><b>A.1</b> Python</a>
<ul>
<li class="chapter" data-level="A.1.1" data-path="installing.html"><a href="installing.html#anaconda"><i class="fa fa-check"></i><b>A.1.1</b> Anaconda</a></li>
</ul></li>
<li class="chapter" data-level="A.2" data-path="installing.html"><a href="installing.html#text-editors"><i class="fa fa-check"></i><b>A.2</b> Text Editors</a>
<ul>
<li class="chapter" data-level="A.2.1" data-path="installing.html"><a href="installing.html#visual-studio-code"><i class="fa fa-check"></i><b>A.2.1</b> Visual Studio Code</a></li>
<li class="chapter" data-level="A.2.2" data-path="installing.html"><a href="installing.html#atom"><i class="fa fa-check"></i><b>A.2.2</b> Atom</a></li>
<li class="chapter" data-level="A.2.3" data-path="installing.html"><a href="installing.html#sublime-text"><i class="fa fa-check"></i><b>A.2.3</b> Sublime Text</a></li>
<li class="chapter" data-level="A.2.4" data-path="installing.html"><a href="installing.html#pycharm"><i class="fa fa-check"></i><b>A.2.4</b> PyCharm</a></li>
</ul></li>
<li class="chapter" data-level="A.3" data-path="installing.html"><a href="installing.html#command-line-tools-bash"><i class="fa fa-check"></i><b>A.3</b> Command Line Tools (Bash)</a>
<ul>
<li class="chapter" data-level="A.3.1" data-path="installing.html"><a href="installing.html#command-line-on-a-mac"><i class="fa fa-check"></i><b>A.3.1</b> Command Line on a Mac</a></li>
<li class="chapter" data-level="A.3.2" data-path="installing.html"><a href="installing.html#command-line-on-windows"><i class="fa fa-check"></i><b>A.3.2</b> Command Line on Windows</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="installing.html"><a href="installing.html#resources-10"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="B" data-path="markdown.html"><a href="markdown.html"><i class="fa fa-check"></i><b>B</b> Markdown</a>
<ul>
<li class="chapter" data-level="B.1" data-path="markdown.html"><a href="markdown.html#writing-markdown"><i class="fa fa-check"></i><b>B.1</b> Writing Markdown</a>
<ul>
<li class="chapter" data-level="B.1.1" data-path="markdown.html"><a href="markdown.html#text-formatting"><i class="fa fa-check"></i><b>B.1.1</b> Text Formatting</a></li>
<li class="chapter" data-level="B.1.2" data-path="markdown.html"><a href="markdown.html#text-blocks"><i class="fa fa-check"></i><b>B.1.2</b> Text Blocks</a></li>
</ul></li>
<li class="chapter" data-level="B.2" data-path="markdown.html"><a href="markdown.html#rendering-markdown"><i class="fa fa-check"></i><b>B.2</b> Rendering Markdown</a></li>
<li class="chapter" data-level="" data-path="markdown.html"><a href="markdown.html#resources-11"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="C" data-path="command-line.html"><a href="command-line.html"><i class="fa fa-check"></i><b>C</b> The Command Line</a>
<ul>
<li class="chapter" data-level="C.1" data-path="command-line.html"><a href="command-line.html#accessing-the-command-line"><i class="fa fa-check"></i><b>C.1</b> Accessing the Command line</a></li>
<li class="chapter" data-level="C.2" data-path="command-line.html"><a href="command-line.html#navigating-the-command-line"><i class="fa fa-check"></i><b>C.2</b> Navigating the Command Line</a>
<ul>
<li class="chapter" data-level="C.2.1" data-path="command-line.html"><a href="command-line.html#changing-directories"><i class="fa fa-check"></i><b>C.2.1</b> Changing Directories</a></li>
<li class="chapter" data-level="C.2.2" data-path="command-line.html"><a href="command-line.html#listing-files"><i class="fa fa-check"></i><b>C.2.2</b> Listing Files</a></li>
<li class="chapter" data-level="C.2.3" data-path="command-line.html"><a href="command-line.html#paths"><i class="fa fa-check"></i><b>C.2.3</b> Paths</a></li>
</ul></li>
<li class="chapter" data-level="C.3" data-path="command-line.html"><a href="command-line.html#file-commands"><i class="fa fa-check"></i><b>C.3</b> File Commands</a>
<ul>
<li class="chapter" data-level="C.3.1" data-path="command-line.html"><a href="command-line.html#learning-new-commands"><i class="fa fa-check"></i><b>C.3.1</b> Learning New Commands</a></li>
<li class="chapter" data-level="C.3.2" data-path="command-line.html"><a href="command-line.html#wildcards"><i class="fa fa-check"></i><b>C.3.2</b> Wildcards</a></li>
</ul></li>
<li class="chapter" data-level="C.4" data-path="command-line.html"><a href="command-line.html#dealing-with-errors"><i class="fa fa-check"></i><b>C.4</b> Dealing With Errors</a></li>
<li class="chapter" data-level="C.5" data-path="command-line.html"><a href="command-line.html#directing-output"><i class="fa fa-check"></i><b>C.5</b> Directing Output</a></li>
<li class="chapter" data-level="C.6" data-path="command-line.html"><a href="command-line.html#shell-scripts"><i class="fa fa-check"></i><b>C.6</b> Shell Scripts</a></li>
<li class="chapter" data-level="" data-path="command-line.html"><a href="command-line.html#resources-12"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="D" data-path="javascript.html"><a href="javascript.html"><i class="fa fa-check"></i><b>D</b> JavaScript</a>
<ul>
<li class="chapter" data-level="D.1" data-path="javascript.html"><a href="javascript.html#programming-with-javascript"><i class="fa fa-check"></i><b>D.1</b> Programming with JavaScript</a>
<ul>
<li class="chapter" data-level="D.1.1" data-path="javascript.html"><a href="javascript.html#history-and-versions"><i class="fa fa-check"></i><b>D.1.1</b> History and Versions</a></li>
<li class="chapter" data-level="D.1.2" data-path="javascript.html"><a href="javascript.html#running-javascript"><i class="fa fa-check"></i><b>D.1.2</b> Running JavaScript</a></li>
</ul></li>
<li class="chapter" data-level="D.2" data-path="javascript.html"><a href="javascript.html#javascript-basics"><i class="fa fa-check"></i><b>D.2</b> JavaScript Basics</a>
<ul>
<li class="chapter" data-level="D.2.1" data-path="javascript.html"><a href="javascript.html#strict-mode"><i class="fa fa-check"></i><b>D.2.1</b> Strict Mode</a></li>
</ul></li>
<li class="chapter" data-level="D.3" data-path="javascript.html"><a href="javascript.html#variables-1"><i class="fa fa-check"></i><b>D.3</b> Variables</a>
<ul>
<li class="chapter" data-level="D.3.1" data-path="javascript.html"><a href="javascript.html#basic-data-types"><i class="fa fa-check"></i><b>D.3.1</b> Basic Data Types</a></li>
<li class="chapter" data-level="D.3.2" data-path="javascript.html"><a href="javascript.html#type-coercion"><i class="fa fa-check"></i><b>D.3.2</b> Type Coercion</a></li>
<li class="chapter" data-level="D.3.3" data-path="javascript.html"><a href="javascript.html#arrays"><i class="fa fa-check"></i><b>D.3.3</b> Arrays</a></li>
<li class="chapter" data-level="D.3.4" data-path="javascript.html"><a href="javascript.html#objects"><i class="fa fa-check"></i><b>D.3.4</b> Objects</a></li>
</ul></li>
<li class="chapter" data-level="D.4" data-path="javascript.html"><a href="javascript.html#control-structures"><i class="fa fa-check"></i><b>D.4</b> Control Structures</a>
<ul>
<li class="chapter" data-level="D.4.1" data-path="javascript.html"><a href="javascript.html#conditionals"><i class="fa fa-check"></i><b>D.4.1</b> Conditionals</a></li>
<li class="chapter" data-level="D.4.2" data-path="javascript.html"><a href="javascript.html#loops-1"><i class="fa fa-check"></i><b>D.4.2</b> Loops</a></li>
</ul></li>
<li class="chapter" data-level="D.5" data-path="javascript.html"><a href="javascript.html#functions-1"><i class="fa fa-check"></i><b>D.5</b> Functions</a>
<ul>
<li class="chapter" data-level="D.5.1" data-path="javascript.html"><a href="javascript.html#functional-programming-1"><i class="fa fa-check"></i><b>D.5.1</b> Functional Programming</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#resources-13"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Introduction to Programming</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="javascript" class="section level1" number="14">
<h1><span class="header-section-number">D</span> JavaScript</h1>
<blockquote>
<p>JavaScript for Snake People</p>
</blockquote>
<p><strong>JavaScript</strong> is a popular, high-level, general-purpose programming language primarily used for implementing interactive web applications and other information systems. JavaScript shares many structural (and even some syntactical) similarities with Python; the places where it differs often draws from other major major languages like Java and C (and thus are good styles to be familiar with). Overall, this makes JavaScript a useful second language to learn: programs will utilize similar structures, but demonstrate how code syntax can be abstracted in more general programming practices.</p>
<p>This chapter introduces the basic syntax of the JavaScript language, including variables, control structures, and functions. <strong>Note</strong> that this introduction assumes familiarity with Python, and introduces concepts in contrast to that languages. For more general purpose introductions, see the below resources.</p>
<div id="programming-with-javascript" class="section level2" number="14.1">
<h2><span class="header-section-number">D.1</span> Programming with JavaScript</h2>
<p>Like Python, JavaScript is a <strong>high-level, general-purpose, interpreted programming language</strong>. The biggest difference is where it is most commonly used: while the Python interpreter is installed on a computer and is usually accessed through the command line (or through the Jupyter program), JavaScript interpreters are most commonly built into web browsers (such as Chrome or Firefox). Browsers are able to download scripts written in JavaScript, executing them line-by-line and use those instructions to manipulate what content is displayed.</p>
<p class="alert alert-info">
It is also possible to execute JavaScript code via the command line by using <a href="https://nodejs.org/en/">Node.js</a>, allowing JavaScript to be a fully general language. However, JavaScript is still most commonly used inside the browser, which is how it will be discussed in this book.
</p>
<div id="history-and-versions" class="section level3" number="14.1.1">
<h3><span class="header-section-number">D.1.1</span> History and Versions</h3>
<p>The JavaScript language was developed by <a href="https://en.wikipedia.org/wiki/Brendan_Eich">Brendan Eich</a> (the co-founder of Mozilla) in 1995 while he was working for Netscape. The original prototype of the language was created in 10 days… a fact which may help explain some of the quirks in the language.</p>
<p>Despite the names, <em>JavaScript</em> and the <em>Java</em> language have nothing to do with one another (and are in fact totally separate programming languages used in drastically different contexts). JavaScript was named after Java as a marketing ploy to cash in on the popularity of the latter.</p>
<p>Like Python, JavaScript continues to be developed through multiple versions, though <em>unlike</em> Python these versions are all backwards compatible. Each new version of JavaScript includes additional syntax shortcuts, specialized keywords, and additional core functions. The main limitation on utilizing new JavaScript features is whether the <em>interpreters</em> found in web browsers are able to support them. This module will use syntax and styles based on <code>ES5</code> (JavaScript 5), which was introduced in 2011 and is supported by all modern browsers (i.e., IE 10 or later).</p>
<ul>
<li><p><code>ES6</code> was introduced in 2015 and provides features that work on <a href="http://kangax.github.io/compat-table/es6/">many browsers</a>, while <code>ES7</code> was finalized in 2016 but is not <a href="http://kangax.github.io/compat-table/esnext/">reliably supported</a></p></li>
<li><p>Technically, JavaScript is an <em>implementation</em> of the the <a href="https://en.wikipedia.org/wiki/ECMAScript">ECMAScript</a> specification (which defines the structure and behaviors of multiple programming languages). Hence the <code>ES</code> in the version names.</p></li>
</ul>
</div>
<div id="running-javascript" class="section level3" number="14.1.2">
<h3><span class="header-section-number">D.1.2</span> Running JavaScript</h3>
<p>JavaScript scripts are executed in a web browser as part of the browser rendering a web page. Since browsers render HTML content (in <code>.html</code> files), JavaScript scripts are specified within that HTML by using a <code><script></code> tag and specifying the <em>relative</em> path to the script file (usually a <strong><code>.js</code></strong> file) to execute. When the HTML being rendered (reading top to bottom) gets to that tag, the browser will download and execute the specified script file using the JavaScript interpreter:</p>
<pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>path/to/my/script.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span></code></pre>
<ul>
<li><p>The <code><script></code> tag can be included anywhere in an HTML page. Most commonly it is either placed in the <code><head></code> in order for the script to be executed <em>before</em> the page content loads, or at the very end of the <code><body></code> in order for the script to be executed <em>after</em> the page content loads (and so can interact with that content).</p></li>
<li><p><strong>IMPORTANT:</strong> note that if you edit the <code>.js</code> script file, you will need to <strong>reload</strong> the page so that the browser can execute the script again (starting from the beginning, as if the page were viewed for the first time).</p></li>
</ul>
<p>A webpage can include multiple <code><script></code> tags, each specifying their own script file. These scripts will be executed by the interpreter whenever they are encountered, top to bottom. And since variables and functions are usually defined <em>globally</em>, this means that any variables or functions created in one script will be available for use in the next (just like how variables created in one Jupyter cell are available in the next).</p>
<p>Thus additional JavaScript modules can be “imported” by including their script file as a <code><script></code> tag <em>before</em> the script that needs to use them (no explicit <code>import</code> statement is necessary). These scripts will then make additional <strong>global variables</strong> available for use by later scripts.</p>
<ul>
<li><p>Moreover, rather than downloading third-party modules as part of a package like Anaconda, the path to third-party JavaScript modules are specified as an internet URI so that the module is downloaded along with the rest of the web page. For example:</p>
<pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>https://d3js.org/d3.v5.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span></code></pre>
<p>will “import” (download and include) the <a href="https://d3js.org/">D3</a> library and provide a global <code>d3</code> variable for later scripts to use—similar to <code>import d3</code> in Python.</p></li>
</ul>
<p>While JavaScript most commonly is used to manipulate the web page content and is thus pretty obvious to the user, it <em>also</em> can produce “terminal-like” output—including printed text and <strong>error messages</strong>. This output can be viewed through the <strong>JavaScript Console</strong> included as a <em>developer tool</em> in the Chrome browser (other browsers include similar tools):</p>
<div class="figure">
<img src="img/javascript/chrome-dev-console.png" alt="" />
<p class="caption">Accessing the developer console in Chrome.</p>
</div>
<p class="alert alert-warning">
<strong>Important:</strong> You should <strong><em>always</em></strong> have the JavaScript Console open when developing JavaScript code, since this is where any error messages will appear!
</p>
<p>Finally, while a web browser is able to open any local <code>.html</code> file and run its included <code>.js</code> scripts, certain interactions (e.g., downloading data from the internet via an HTTP request) may require a web page to be provided from a <em>web server</em> via the <code>http://</code> protocol, rather than the <code>file://</code> protocol. Luckily Python 3 provides a simple web server through the <code>http.server</code> module which you can use to “serve” the files to yourself:</p>
<pre class="language-bash"><code><span class="token comment"># on the command line:</span>
<span class="token builtin class-name">cd</span> path/to/project
python -m http.server</code></pre>
<p>The served webpage will be available at <a href="http://localhost:8000/" class="uri">http://localhost:8000/</a> (“localhost” means “this machine”).</p>
<ul>
<li>Be sure to <code>cd</code> to the directory that contains the <code>index.html</code> file—otherwise you will be serving from a different folder so will need to adjust the URL’s path.</li>
<li>The <code>-m</code> option mean to run the built-in module as script, instead of just importing it.</li>
<li>I highly recommend you run a local web server whenever you are doing web development!</li>
</ul>
</div>
</div>
<div id="javascript-basics" class="section level2" number="14.2">
<h2><span class="header-section-number">D.2</span> JavaScript Basics</h2>
<p>JavaScript scripts (<code>.js</code> files) have a similar overall structure to Python scripts: these files contain lines of code, each of which is a <strong>statement</strong> (instruction). The JavaScript interpreter executes each statement one at a time, top to bottom (responding to control structures and function calls of course).</p>
<p>Like Python, JavaScript code can include <strong>comments</strong>, which is text that is <em>ignored</em> by the interpreter. JavaScript includes two kinds of comments:</p>
<ul>
<li>Anything written after two slashes (<code>//</code>) until the end of the line is treated as an <em>inline comment</em> and ignored.</li>
<li>Anything written between <code>/*</code> and <code>*/</code> is treated as a <em>block comment</em> and ignored. Block comments can span multiple lines.</li>
</ul>
<pre class="language-js"><code><span class="token comment">/* This is a block comment
It can span multiple lines */</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Hello world!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//this is an inline comment (like # in Python)</span></code></pre>
<p>The above example code demonstrates the <strong><code>console.log()</code></strong> function, which is JavaScript’s equivalent to <code>print()</code>—the output will be shown in the JavaScript console (in the Developer Tools). Thus we talk about “logging out” values in JavaScript, instead of “printing” values in Python.</p>
<ul>
<li>The <code>console.log()</code> function is technically a <code>log()</code> method belonging being called on a global <code>console</code> object.</li>
</ul>
<p>Also notice that the example statement ends in a semicolon (<strong><code>;</code></strong>). All JavaScript statements end in semicolons, marking that statement as being finished (like a period in an English sentence). Unlike in Python, statements that do not end in semicolons can be allowed to continue onto the next line.</p>
<ul>
<li>Note that JavaScript tries to helpful and will often assume that statements end at the end of a line if the next line “looks like” a new statement. However, it occasionally screws up—and so best practice as a developer is to <strong>always include the semicolons</strong>.</li>
</ul>
<div id="strict-mode" class="section level3" number="14.2.1">
<h3><span class="header-section-number">D.2.1</span> Strict Mode</h3>
<p>ES5 includes the ability for JavaScript to be interpreted in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode"><strong>strict mode</strong></a>. Strict mode is more “strict” about how the interpreter understands the syntax: it is less likely to assume that certain programmer mistakes were intentional (and so try to run the code anyway). For example, in <em>strict mode</em> the interpreter will produce an <em>Error</em> if you try and use a variable that has not yet been defined, while without strict mode the code will just use an <code>undefined</code> value. Thus working in strict mode can help catch a lot of silly mistakes.</p>
<p>You declare that a script or function should be executed in strict mode by putting an interpreter declaration at the top of the file:</p>
<pre class="language-js"><code><span class="token string">'use strict'</span><span class="token punctuation">;</span></code></pre>
<p>This is not a String, but rather a <em>declaration</em> to the interpreter about how it should be behave.</p>
<p class="alert alert-warning">
<strong>ALWAYS USE STRICT MODE!</strong> It will help avoid typo-based bugs, as well as enable your code to run more efficiently.
</p>
</div>
</div>
<div id="variables-1" class="section level2" number="14.3">
<h2><span class="header-section-number">D.3</span> Variables</h2>
<p>JavaScript variables are <strong>declared</strong> using the <code>var</code> keyword. This is like creating the “nametag”; once it has been declared, new values can be assigned to that variable without further declaration. Declared variables have a default value of <code>undefined</code>—a value representing that the variable has no value (similar to <code>None</code> in Python).</p>
<pre class="language-js"><code><span class="token keyword">var</span> x <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span> <span class="token comment">//declare and assign value</span>
<span class="token keyword">var</span> y <span class="token operator">=</span> x<span class="token punctuation">;</span> <span class="token comment">//declare and assign value</span>
x <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span> <span class="token comment">//assign value to existing variable</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>x<span class="token operator">+</span><span class="token string">', '</span><span class="token operator">+</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5, 4</span>
<span class="token keyword">var</span> z<span class="token punctuation">;</span> <span class="token comment">//declare variable (not assigned)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>z<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//undefined</span></code></pre>
<p>JavaScript variables are conventionally named using <a href="https://en.wikipedia.org/wiki/Camel_case">camelCase</a>: each word in the variable name is put together (without a separating <code>_</code>), with the first letter in subsequent words capitalized:</p>
<pre class="language-js"><code><span class="token keyword">var</span> myVariable <span class="token operator">=</span> <span class="token number">598</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> numberOfDaysInAYear <span class="token operator">=</span> <span class="token number">365.25</span><span class="token punctuation">;</span></code></pre>
<div class="alert alert-info">
<p><code>ES6</code> introduces the keyword <strong><code>let</code></strong> for declaring variables (to use instead of <code>var</code>). This works in a similar fashion, except that variables declared using <code>let</code> are <strong>“block scoped”</strong>, meaning that they are only available within the block in which they are defined, not the entire function. This is different than how Python works, but similar to how Java and C work. The <code>let</code> keyword is available in all modern browsers, and is increasingly common in JavaScript examples and tutorials. For this purposes of this book, you can view it as an interchangeable option with <code>var</code>.</p>
</div>
<div id="basic-data-types" class="section level3" number="14.3.1">
<h3><span class="header-section-number">D.3.1</span> Basic Data Types</h3>
<p>JavaScript supports the similar basic data types as Python:</p>
<ul>
<li><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number"><strong>Numbers</strong></a> are used to represent numeric data (JavaScript does not distinguish between integers and floats). Numbers support most of the same <em>mathematical</em> and operators as Python (you can’t use <code>**</code> to raise to an exponent, and <code>//</code> is a comment not integer division). Common mathematical functions can be accessed through in the built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math"><code>Math</code></a> global.</p>
<pre class="language-js"><code><span class="token keyword">var</span> x <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> x<span class="token punctuation">;</span> <span class="token comment">//'number'</span>
<span class="token keyword">var</span> y <span class="token operator">=</span> x<span class="token operator">/</span><span class="token number">4</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> y<span class="token punctuation">;</span> <span class="token comment">//'number'</span>
<span class="token comment">//Numbers use floating point division</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>x<span class="token operator">/</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//1.25</span>
<span class="token comment">//Use the Math.floor() function to do integer division</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>x<span class="token operator">/</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//1</span>
<span class="token comment">//Other common Math functions are available as well</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">sqrt</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2.23606797749979</span></code></pre></li>
<li><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String"><strong>Strings</strong></a> can be written in either single quotes (<strong><code>'</code></strong>) or double quotes (<strong><code>"</code></strong>), but most <a href="https://google.github.io/styleguide/jsguide.html">style guidelines</a> recommend single quotes. Strings can be concatenated (but not multiplied!)</p>
<pre class="language-js"><code><span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token string">'Joel'</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> greeting <span class="token operator">=</span> <span class="token string">'Hello, my name is '</span><span class="token operator">+</span>name<span class="token punctuation">;</span> <span class="token comment">//concatenation</span></code></pre>
<p>Strings also support many <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">methods</a> for working with them. Note that like Python, JavaScript strings are <em>immutable</em>, so most string methods return a new, altered string.</p></li>
<li><p><strong>Booleans</strong> in JavaScript are written in lowercase letters: <code>true</code> and <code>false</code>. Booleans can be produced using the same <em>relational operators</em> as in Python. However, the <em>logical operators</em> used on booleans are written using symbols rather than words: two <em>ampersands</em> (<strong><code>&&</code></strong>) mean “and”, two <em>pipes</em> (<strong><code>||</code></strong>) mean “or”, and an exclamation point (<strong><code>!</code></strong>) means “not”:</p>
<pre class="language-js"><code><span class="token comment">//conjunction</span>
boolOne <span class="token operator">&&</span> boolTwo <span class="token comment">//and (two ampersands)</span>
<span class="token comment">//disjunction</span>
boolOne <span class="token operator">||</span> boolTwo <span class="token comment">//or (two pipes)</span>
<span class="token comment">//negation</span>
<span class="token operator">!</span>boolOne <span class="token comment">//not (exclamation point)</span>
<span class="token comment">//combining</span>
<span class="token constant">P</span> <span class="token operator">||</span> <span class="token operator">!</span><span class="token constant">Q</span> <span class="token comment">//P or not Q</span>
<span class="token operator">!</span><span class="token constant">P</span> <span class="token operator">&&</span> <span class="token constant">Q</span> <span class="token comment">//not P and also Q</span>
<span class="token operator">!</span><span class="token punctuation">(</span><span class="token constant">P</span> <span class="token operator">&&</span> <span class="token constant">Q</span><span class="token punctuation">)</span> <span class="token comment">//not (P and Q both)</span>
<span class="token punctuation">(</span><span class="token operator">!</span><span class="token constant">P</span><span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token constant">Q</span><span class="token punctuation">)</span> <span class="token comment">//not P or not Q</span>
<span class="token number">3</span> <span class="token operator"><</span> x <span class="token operator">&&</span> x <span class="token operator"><</span> <span class="token number">5</span> <span class="token comment">//not as cool as Python</span></code></pre></li>
</ul>
</div>
<div id="type-coercion" class="section level3" number="14.3.2">
<h3><span class="header-section-number">D.3.2</span> Type Coercion</h3>
<p>Unlike Python, JavaScript will not throw errors if you try to apply operators (such as <code>+</code> or <code><</code>) to different types. Instead, the interpreter will try to be “helpful” and <strong>coerce</strong> (convert) a value from one data type into another. While this means you don’t have to explicitly use <code>str()</code> in order to print numbers, JavaScript’s type coercion can produce a few quirks:</p>
<pre class="language-js"><code><span class="token keyword">var</span> x <span class="token operator">=</span> <span class="token string">'40'</span> <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> '402'; the 2 is coerced to a String</span>
<span class="token keyword">var</span> y <span class="token operator">=</span> <span class="token string">'40'</span> <span class="token operator">-</span> <span class="token number">4</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> 36; can't subtract strings so '40' is coerced to a Number!</span></code></pre>
<p>JavaScript will also attempt to coerce values when checking for equality with <code>==</code>:</p>
<pre class="language-js"><code><span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">10</span>
<span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'10'</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>num <span class="token operator">==</span> str<span class="token punctuation">)</span> <span class="token comment">//true, the values can be coerced into one another</span></code></pre>
<p>In this case, the interpreter will coerce the Number <code>10</code> into the String <code>'10'</code> (since numbers can always be made into Strings), and since those Strings are the same, determines that the variables are equal.</p>
<p>In general this type of automatic coercion can lead to subtle bugs. Thus you should instead always use the <strong><code>===</code></strong> operator (and it’s partner <code>!==</code>), which checks both value <em>and</em> type for equality:</p>
<pre class="language-js"><code><span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">10</span>
<span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'10'</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>num <span class="token operator">===</span> str<span class="token punctuation">)</span> <span class="token comment">//false, the values have different types</span></code></pre>
<p>JavaScript will do its best to coerce any value when compared. Often this means converting values to Strings, but it will also commonly convert values into <em>booleans</em> to compare them. So for example:</p>
<pre class="language-js"><code><span class="token comment">//compare an empty String to the number 0</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> <span class="token string">''</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//true; both can be coerced to a `false` value</span></code></pre>
<p>This is because both the empty string <code>''</code> and <code>0</code> are considered <a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy"><strong>“falsey”</strong></a> values (values that can be coerced to <code>false</code>). Other falsy values include <code>undefined</code>, <code>null</code>, and <code>NaN</code> (not a number). All other values will be coerced to <code>true</code>.</p>
</div>
<div id="arrays" class="section level3" number="14.3.3">
<h3><span class="header-section-number">D.3.3</span> Arrays</h3>
<p><strong>Arrays</strong> are <em>ordered, one-dimensional sequences of values</em>—very similar to Python lists. They are written as literals inside square brackets <strong><code>[]</code></strong>. Individual elements can be accessed by (0-based) <em>index</em> using <strong>bracket notation</strong>.</p>
<pre class="language-js"><code><span class="token keyword">var</span> names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'John'</span><span class="token punctuation">,</span> <span class="token string">'Paul'</span><span class="token punctuation">,</span> <span class="token string">'George'</span><span class="token punctuation">,</span> <span class="token string">'Ringo'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> letters <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> things <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'raindrops'</span><span class="token punctuation">,</span> <span class="token number">2.5</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> empty <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> names<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// "Paul"</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> things<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 8</span>
letters<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'z'</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> letters <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// ['z', 'b', 'c']</span></code></pre>
<p>Note that it is possible to assign a value to <em>any</em> index in the array, even if that index is “out of bounds”. This will <em>grow</em> the array (increase its length) to include that index—intermediate indices will be given values of <code>undefined</code>. The <em>length</em> of the array (accessed via the <code>.length</code> attribute) will always be the index of the “last” element + 1, even if there are fewer defined values within the array.</p>
<pre class="language-js"><code><span class="token keyword">var</span> letters <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>letters<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 3</span>
letters<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'f'</span><span class="token punctuation">;</span> <span class="token comment">//grows the array</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>letters<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [ 'a', 'b', 'c', , , 'f' ]</span>
<span class="token comment">//blank spaces are undefined</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>letters<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 6</span></code></pre>
<p>Arrays also support a variety of <a href="https://www.w3schools.com/jsref/jsref_obj_array.asp">methods</a> that can be used to easily modify their elements. Common <em>functional programming</em>-style methods are described below.</p>
</div>
<div id="objects" class="section level3" number="14.3.4">
<h3><span class="header-section-number">D.3.4</span> Objects</h3>
<p><strong>Objects</strong> are <em>unordered, one-dimensional sequences of <strong>key-value pairs</strong></em>—very similar to Python dictionaries. They are written as literals inside curly braces <strong><code>{}</code></strong>, with keys and values separated by colons <strong><code>:</code></strong>. Note that in JavaScript, string keys do <em>not</em> need to be written in quotes (the quotes are implied—the keys are in fact strings).</p>
<pre class="language-js"><code><span class="token comment">//an object of ages (explicit Strings for keys)</span>
<span class="token comment">//The `ages` object has a `sarah` property (with a value of 42)</span>
<span class="token keyword">var</span> ages <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string-property property">'sarah'</span><span class="token operator">:</span><span class="token number">42</span><span class="token punctuation">,</span> <span class="token string-property property">'amit'</span><span class="token operator">:</span><span class="token number">35</span><span class="token punctuation">,</span> <span class="token string-property property">'zhang'</span><span class="token operator">:</span><span class="token number">13</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">//different properties can have the same values</span>
<span class="token comment">//property names with non-letter characters must be in quotes</span>
<span class="token keyword">var</span> meals <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">breakfast</span><span class="token operator">:</span><span class="token string">'coffee'</span><span class="token punctuation">,</span> <span class="token literal-property property">lunch</span><span class="token operator">:</span> <span class="token string">'coffee'</span><span class="token punctuation">,</span> <span class="token string-property property">'afternoon tea'</span><span class="token operator">:</span> <span class="token string">'coffee'</span><span class="token punctuation">}</span>
<span class="token comment">//values can be of different types (including arrays or other objects!)</span>
<span class="token keyword">var</span> typeExamples <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">number</span><span class="token operator">:</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token literal-property property">string</span><span class="token operator">:</span><span class="token string">'dog'</span><span class="token punctuation">,</span> <span class="token literal-property property">array</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">//objects can be empty (contains no properties)</span>
<span class="token keyword">var</span> empty <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></code></pre>
<p>Object elements are also known as <strong>properties</strong>. For example, we say that the <code>ages</code> object has a <code>sarah</code> property (with a value of <code>42</code>).</p>
<p>Object values can be access via <strong>bracket</strong> notation, specifying the <em>key</em> as the index. If a key does not have an explicit value associated with it, accessing that key produces <code>undefined</code> (the key’s value is <code>undefined</code>).</p>
<pre class="language-js"><code><span class="token keyword">var</span> favorites <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">music</span><span class="token operator">:</span> <span class="token string">'jazz'</span><span class="token punctuation">,</span> <span class="token literal-property property">food</span><span class="token operator">:</span> <span class="token string">'pizza'</span><span class="token punctuation">,</span> <span class="token literal-property property">numbers</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">//access variable</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> favorites<span class="token punctuation">[</span><span class="token string">'music'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//'jazz'</span>
<span class="token comment">//assign variable</span>
favorites<span class="token punctuation">[</span><span class="token string">'food'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'cake'</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> favorites<span class="token punctuation">[</span><span class="token string">'food'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//'cake'</span>
<span class="token comment">//access undefined key</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> favorites<span class="token punctuation">[</span><span class="token string">'language'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//undefined</span>
favorites<span class="token punctuation">[</span><span class="token string">'language'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'javascript'</span><span class="token punctuation">;</span> <span class="token comment">//assign new key and value</span>
<span class="token comment">//access nested values</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> favorites<span class="token punctuation">[</span><span class="token string">'numbers'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//12</span></code></pre>
<p><em>Additionally</em>, object values can also be accessed via <strong>dot notation</strong>, as if the properties were <em>attributes</em> of a class. This is often simpler to write and to read: remember to read the <strong><code>.</code></strong> as an <code>'s</code>!</p>
<pre class="language-js"><code><span class="token keyword">var</span> favorites <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">music</span><span class="token operator">:</span> <span class="token string">'jazz'</span><span class="token punctuation">,</span> <span class="token literal-property property">food</span><span class="token operator">:</span> <span class="token string">'pizza'</span><span class="token punctuation">,</span> <span class="token literal-property property">numbers</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">//access variable</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> favorites<span class="token punctuation">.</span>music <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//'jazz'</span>
<span class="token comment">//assign variable</span>
favorites<span class="token punctuation">.</span>food <span class="token operator">=</span> <span class="token string">'cake'</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> favorites<span class="token punctuation">.</span>food <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//'cake'</span>
<span class="token comment">//access undefined key</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> favorites<span class="token punctuation">.</span>language <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//undefined</span>
favorites<span class="token punctuation">.</span>language <span class="token operator">=</span> <span class="token string">'javascript'</span><span class="token punctuation">;</span> <span class="token comment">//assign new key and value</span>
<span class="token comment">//access nested values</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> favorites<span class="token punctuation">.</span>numbers<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//12</span></code></pre>
<ul>
<li>The one advantage to using <em>bracket notation</em> is that you can specify property names as variables or the results of an expression. Thus the recommendation is to use <em>dot notation</em> unless the property you wish to access is dynamically determined.</li>
</ul>
<p>It is possible to get <em>arrays</em> of the object’s keys calling the <code>Object.keys()</code> method and passing in the object you wish to get the keys of. Note that an equivalent function for values is not supported by most browsers.</p>
<pre class="language-js"><code><span class="token keyword">var</span> ages <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">sarah</span><span class="token operator">:</span><span class="token number">42</span><span class="token punctuation">,</span> <span class="token literal-property property">amit</span><span class="token operator">:</span><span class="token number">35</span><span class="token punctuation">,</span> <span class="token literal-property property">zhang</span><span class="token operator">:</span><span class="token number">13</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> keys <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>ages<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [ 'sarah', 'amit', 'zhang' ]</span></code></pre>
</div>
</div>
<div id="control-structures" class="section level2" number="14.4">
<h2><span class="header-section-number">D.4</span> Control Structures</h2>
<p>JavaScript supports <strong>control structures</strong> (conditional <code>if</code> statements, <code>while</code> and <code>for</code> loops) in the same way that Python does, just with a slightly different syntax.</p>
<div id="conditionals" class="section level3" number="14.4.1">
<h3><span class="header-section-number">D.4.1</span> Conditionals</h3>
<p>A JavaScript <strong><code>if</code> statement</strong> is written using the following syntax:</p>
<pre class="language-js"><code><span class="token keyword">if</span><span class="token punctuation">(</span>condition<span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token comment">//statements</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>alternativeCondition<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//statements</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token comment">//statements</span>
<span class="token punctuation">}</span></code></pre>
<p>In JavaScript, a <strong>block</strong> of statements (e.g., to conditionally execute) is written inside curly braces <strong><code>{}</code></strong>. JavaScript doesn’t use indentation to designate blocks, though you should still indent blocks for readability.
- As in Python, the <code>else if</code> and <code>else</code> clauses are optional.</p>
<p>Similar to Python, the <strong><code>condition</code></strong> can be any expression that evaluates to a Boolean value, and is placed inside parentheses. But since any value can be <em>coerced</em> into Booleans, you can put any value you want inside the <code>if</code> condition. This is actually really useful—since <code>undefined</code> is a falsy value, you can use this coercion to check if a variable has been assigned or not:</p>
<pre class="language-js"><code><span class="token comment">//check if a `person` variable has a `name` property</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Person does have a name!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>In the above example, the condition will only coerce to <code>true</code> if <code>person.name</code> is defined (<em>not</em> <code>undefined</code>) and is not empty. If somehow the variable has not been assigned (e.g., the user didn’t fill out the form completely), then the condition will not be true.</p>
<p>Overall, while the syntax may be different, the control flow impacts are the same as in Python: conditional statements can be nested, and subsequent <code>if</code> statements are not necessarily mutually exclusive!</p>
</div>
<div id="loops-1" class="section level3" number="14.4.2">
<h3><span class="header-section-number">D.4.2</span> Loops</h3>
<p>JavaScript supports <strong><code>while</code> loops</strong> just like Python, with the syntactic change that conditions are listed in parentheses and blocks are written in curly braces:</p>
<pre class="language-js"><code><span class="token keyword">var</span> count <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>count <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
count <span class="token operator">=</span> count <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Blastoff!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>JavaScript also provides a <strong><code>for</code> loop</strong> used for <em>definite iteration</em>, such as when you know how many times you want to continue through the loop. To understand the JavaScript <code>for</code> loop, consider the below loop for iterating through an array:</p>
<pre class="language-js"><code><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>i <span class="token operator"><</span> array<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
i<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token comment">//shortcut for i = i+1</span>
<span class="token punctuation">}</span></code></pre>
<p>This loop has somewhat generic parts:</p>
<ol style="list-style-type: decimal">
<li>It initializes the <em>loop control variable</em> (<code>var i=0</code>)</li>
<li>It checks the condition each time through the loop (<code>i < array.length</code>)</li>
<li>It <em>increments</em> the loop control variable at the end of the block (<code>i++</code>). The <code>++</code> is a short cut for “add 1 and reassign” (since statements such as <code>i=i+1</code> are so common)</li>
</ol>
<p>The JavaScript <code>for</code> loop combines these three steps into a single statement:</p>
<pre class="language-js"><code><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator"><</span>array<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>Thus JavaScript makes it easy to keep track of a loop control variable (a counter), though you need to write out the initialization, condition, and update yourself.</p>
<ul>
<li>This is equivalent to the Python <code>for i in range(len(array)):</code></li>
</ul>
<div class="alert alert-info">
<p><strong>Warning</strong> JavaScript <em>does</em> have a <code>for ... in</code> syntax. However, it doesn’t work as you would expect for arrays (it iterates over “enumerable properties” rather than the specific indices), and so should <strong>not</strong> be used with arrays. Instead, <code>ES6</code> introduces a <code>for ... of</code> syntax for iterating over arrays, though it is not supported by all browsers. Thus the current best practice is to use the above <code>for</code> loop, or better yet the <code>forEach()</code> method described below.</p>
<p>If you need to iterate over the keys of an object, use the <code>Object.keys()</code> method to get an array to loop through!</p>
</div>
</div>
</div>
<div id="functions-1" class="section level2" number="14.5">
<h2><span class="header-section-number">D.5</span> Functions</h2>
<p>And of course, JavaScript includes <strong>functions</strong> (named sequences of statements used to <em>abstract</em> code) just like Python. JavaScript functions are written using the following syntax:</p>
<pre class="language-js"><code><span class="token comment">//A function named `makeFullName` that takes two arguments</span>
<span class="token comment">//and returns the "full name" made from them</span>
<span class="token keyword">function</span> <span class="token function">makeFullName</span><span class="token punctuation">(</span><span class="token parameter">firstName<span class="token punctuation">,</span> lastName</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//Function body: perform tasks in here</span>
<span class="token keyword">var</span> fullName <span class="token operator">=</span> firsName <span class="token operator">+</span> <span class="token string">" "</span> <span class="token operator">+</span> lastName<span class="token punctuation">;</span>
<span class="token comment">// Return: what you want the function to output</span>
<span class="token keyword">return</span> fullName<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// Call the makeFullName function with the values "Alice" and "Kim"</span>
myName <span class="token operator">=</span> <span class="token function">makeFullName</span><span class="token punctuation">(</span><span class="token string">"Alice"</span><span class="token punctuation">,</span> <span class="token string">"Kim"</span><span class="token punctuation">)</span> <span class="token comment">// "Alice Kim"</span></code></pre>
<p>Things to note about this syntax:</p>
<ul>
<li><p>Functions are defined by using the <strong><code>function</code></strong> keyword (placed before the name of the function). This acts similar to <code>def</code>.</p></li>
<li><p>As with control structures, the <strong>block</strong> that is the function’s <em>body</em> is written in curly braces <strong><code>{}</code></strong>.</p></li>
</ul>
<p>Like Python, JavaScript variables are <em>scoped</em> to functions: any variables declared within a function will not be available outside of it. Functions are able to access, modify, and assign variables that are at a <em>higher</em> scope (e.g., global variables). This includes the arguments, which are implicitly declared <em>local</em> variables!</p>
<p>JavaScript does not support named keyword arguments (before <code>ES6</code> anyway). However, in JavaScript <strong>all arguments are optional</strong>. Any argument that is <em>not</em> (positionally) passed a specific value will be <code>undefined</code>. Any passed in value that does not have a variable defined for its position will not be assigned to a variable.</p>
<pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token string">"Hello, "</span><span class="token operator">+</span>name<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//expected: argument is assigned a value</span>
<span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token string">"Joel"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//"Hello, Joel"</span>
<span class="token comment">//argument not assigned value (left undefined)</span>
<span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//"Hello, undefined"</span>
<span class="token comment">//extra arguments (values) are not assigned to variables, so are ignored</span>
<span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token string">"Joel"</span><span class="token punctuation">,</span><span class="token string">"y'all"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//"Hello, Joel"</span></code></pre>
<ul>
<li>If a function has an argument, that doesn’t mean it got a value. If a function lacks an argument, that doesn’t mean it wasn’t given a value.</li>
</ul>
<div id="functional-programming-1" class="section level3" number="14.5.1">
<h3><span class="header-section-number">D.5.1</span> Functional Programming</h3>
<p><strong>JavaScript functions ARE values</strong>. This means that, as in Python, each function you define is in fact an object that can be assigned to other variables or passed around to other functions.</p>
<pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Hello, "</span><span class="token operator">+</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//what kind of thing is `sayHello` ?</span>
<span class="token keyword">typeof</span> sayHello<span class="token punctuation">;</span> <span class="token comment">//'function'</span>
<span class="token comment">//assign the `sayHello` value to a new variable `greet`</span>
<span class="token keyword">var</span> greet <span class="token operator">=</span> sayHello<span class="token punctuation">;</span>
<span class="token comment">//call the function assigned to the `greet` variable</span>
<span class="token function">greet</span><span class="token punctuation">(</span><span class="token string">"world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//prints "Hello world"</span></code></pre>
<p>Just like arrays and objects can be written as literals which can be <em>anonymously</em> passed into functions, JavaScript supports <strong>anonymous functions</strong> (similar to Python’s anonymous lambdas):</p>
<pre class="language-js"><code><span class="token comment">//Example: an anonymous function (no name!)</span>
<span class="token comment">//(We can't reference this without a name, so writing an anonymous function is</span>
<span class="token comment">//not a valid statement)</span>
<span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">person</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Hello, "</span><span class="token operator">+</span>person<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//Anonymous function (value) assigned to variable</span>
<span class="token comment">//equivalent to the version in the previous example</span>
<span class="token keyword">var</span> <span class="token function-variable function">sayHello</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">person</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Hello, "</span><span class="token operator">+</span>person<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>You can think of this structure as equivalent to declaring and assigning an array <code>var myVar = [1,2,3]</code>… just in this case instead of taking the anonymous array (right-hand side) and giving it a name, you’re taking an <em>anonymous function</em> and giving it a name!</p>
<p>Using <strong>anonymous callback functions</strong> are incredibly important in JavaScript programming. Many common built-in functions use callback functions to specify some type of behavior, and these callbacks are normally specified as anonymous functions (rather than clutter the namespace).</p>
<div id="array-loops" class="section level4" number="14.5.1.1">
<h4><span class="header-section-number">D.5.1.1</span> Array Loops</h4>
<p>As a common example, <strong>arrays</strong> support a variety of methods for performing <em>functional looping</em>—in fact, this is the recommended way to loop through an array!</p>
<p>We loop through an array by calling the [<code>forEach(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)</code>] function on that array. This function takes in a <em>callback function</em> specifying what block of code to execute “for each” element:</p>
<pre class="language-js"><code><span class="token keyword">var</span> array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//an array</span>
<span class="token keyword">var</span> <span class="token function-variable function">printItem</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//something to do to an array item</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
array<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>printItem<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//do the "printItem" thing to EACH array item</span>
<span class="token comment">//it is much more common to use anonymous function</span>
array<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<ul>
<li><p>This is the (admittedly verbose) equivalent to Python’s <code>for ... in</code> syntax.</p></li>
<li><p>In the last line, we basically take the anonymous function assigned to <code>printItem</code> and “copy-paste” that literal definition as the argument to <code>forEach()</code>, instead of referring to the value by its name. This statement can be read as “take the <code>array</code> and <code>forEach</code> thing run this <code>function</code> on the <code>item</code>”.</p></li>
<li><p>Be careful about the closing braces and parentheses at the end of the anonymous function version: the brace is ending the anonymous function’s block (and so ending that value), and the parenthesis is closing the argument list of <code>forEach()</code>!</p></li>
<li><p>Fun fact: ES6 provides a syntactical shortcut for specifying anonymous functions that is less verbose called <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow functions</a>.</p></li>
</ul>
<p>Arrays also provide <code>map()</code>, <code>filter()</code> and <code>reduce()</code> functions (similar to those in Python) that can be used to do functional-style programming and mirror the functionality provided by Python list comprehensions:</p>
<pre class="language-js"><code><span class="token keyword">var</span> array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">//map: return a new array of transformed items</span>
array<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> item<span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment">//transform each item into its double</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [6, 2, 8, 4, 10]</span>
<span class="token comment">//filter: return a new array of items that meet the criteria</span>
array<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">return</span> item <span class="token operator">>=</span> <span class="token number">3</span><span class="token punctuation">;</span> <span class="token comment">//keep if large</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [3, 4, 5]</span>
<span class="token comment">//reduce: aggregate array elements into a single value</span>
<span class="token comment">//reduce() also takes a second argument which is the starting value</span>
array<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">runningTotal<span class="token punctuation">,</span> item</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">var</span> newTotal <span class="token operator">=</span> runningTotal <span class="token operator">+</span> item<span class="token punctuation">;</span>
<span class="token keyword">return</span> newTotal<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>This type of functional programming is incredibly important when developing JavaScript programs (and especially using it for interactive applications)!</p>
</div>
</div>
</div>
<div id="resources-13" class="section level2 unnumbered">
<h2>Resources</h2>
<p>As the language used for web programming, JavaScript may have more freely available online learning resources than any other programming language! Some of my favorites include:</p>
<div class="list-condensed">
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript">A Re-Introduction to JavaScript</a> a focused tutorial on the primariy language features</li>
<li><a href="https://github.com/getify/You-Dont-Know-JS">You Don’t Know JS</a> a free textbook covering all aspects of the JavaScript language. Very readable and thorough, with lots of good examples.</li>
<li><a href="http://jsforcats.com/">JavaScript for Cats</a> a gentler introduction for “Scaredy-Cats”</li>
<li><a href="https://www.w3schools.com/js/">w3Schools JavaScript Reference</a> a super-friendly reference for the language</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript">MDN JavaScript Reference</a> a complete documentation of JavaScript, including tutorials</li>
<li><a href="https://info343.github.io/">Client Side Web Development</a> a more extensive treatment of JavaScript; see Chapters 10-15.
<!-- - [Google's JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html) --></li>
</ul>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="command-line.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": false,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["github", "facebook", "twitter", "google"],
"google": false
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/infx511/book/edit/master/javascript.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": null,
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "section",
"scroll_highlight": true
}
});
});
</script>
</body>
</html>