-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjavascript.html
More file actions
1113 lines (1036 loc) · 134 KB
/
javascript.html
File metadata and controls
1113 lines (1036 loc) · 134 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en" xml:lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 10 JavaScript Fundamentals | Client-Side Web Development</title>
<meta name="description" content="The course reader for INFO 340: Client-Side Web Development." />
<meta name="generator" content="bookdown 0.24 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 10 JavaScript Fundamentals | Client-Side Web Development" />
<meta property="og:type" content="book" />
<meta property="og:url" content="https://info340.github.io/" />
<meta property="og:image" content="https://info340.github.io//img/cover-img.png" />
<meta property="og:description" content="The course reader for INFO 340: Client-Side Web Development." />
<meta name="github-repo" content="info340/book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 10 JavaScript Fundamentals | Client-Side Web Development" />
<meta name="twitter:description" content="The course reader for INFO 340: Client-Side Web Development." />
<meta name="twitter:image" content="https://info340.github.io//img/cover-img.png" />
<meta name="author" content="Joel Ross" />
<meta name="date" content="2025-03-10" />
<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/busy-spider-icon.png" type="image/x-icon" />
<link rel="prev" href="css-frameworks.html"/>
<link rel="next" href="functional-programming.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/fuse.js@6.4.6/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>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-98444716-3', 'auto');
ga('send', 'pageview');
</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">Client-Side Web Development</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 the Book</a></li>
<li class="chapter" data-level="1" data-path="software-setup.html"><a href="software-setup.html"><i class="fa fa-check"></i><b>1</b> Getting Setup</a>
<ul>
<li class="chapter" data-level="1.1" data-path="software-setup.html"><a href="software-setup.html#web-browser"><i class="fa fa-check"></i><b>1.1</b> Web Browser</a></li>
<li class="chapter" data-level="1.2" data-path="software-setup.html"><a href="software-setup.html#code-editor"><i class="fa fa-check"></i><b>1.2</b> Code Editor</a>
<ul>
<li class="chapter" data-level="" data-path="software-setup.html"><a href="software-setup.html#visual-studio-code"><i class="fa fa-check"></i>Visual Studio Code</a></li>
<li class="chapter" data-level="1.2.1" data-path="software-setup.html"><a href="software-setup.html#other-editors"><i class="fa fa-check"></i><b>1.2.1</b> Other Editors</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="software-setup.html"><a href="software-setup.html#bash-command-line"><i class="fa fa-check"></i><b>1.3</b> Bash (Command Line)</a></li>
<li class="chapter" data-level="1.4" data-path="software-setup.html"><a href="software-setup.html#node-and-npm"><i class="fa fa-check"></i><b>1.4</b> Node and <code>npm</code></a>
<ul>
<li class="chapter" data-level="" data-path="software-setup.html"><a href="software-setup.html#installing-software-with-npm"><i class="fa fa-check"></i>Installing software with npm</a></li>
<li class="chapter" data-level="" data-path="software-setup.html"><a href="software-setup.html#managing-local-packages"><i class="fa fa-check"></i>Managing local packages</a></li>
</ul></li>
<li class="chapter" data-level="1.5" data-path="software-setup.html"><a href="software-setup.html#git-and-github"><i class="fa fa-check"></i><b>1.5</b> Git and GitHub</a></li>
<li class="chapter" data-level="" data-path="software-setup.html"><a href="software-setup.html#resources"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="client-side-development.html"><a href="client-side-development.html"><i class="fa fa-check"></i><b>2</b> Client-Side Development</a>
<ul>
<li class="chapter" data-level="2.1" data-path="client-side-development.html"><a href="client-side-development.html#clients-and-servers"><i class="fa fa-check"></i><b>2.1</b> Clients and Servers</a></li>
<li class="chapter" data-level="2.2" data-path="client-side-development.html"><a href="client-side-development.html#urls-and-web-files"><i class="fa fa-check"></i><b>2.2</b> URLs and Web Files</a>
<ul>
<li class="chapter" data-level="" data-path="client-side-development.html"><a href="client-side-development.html#client-side-file-types"><i class="fa fa-check"></i>Client-Side File Types</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="client-side-development.html"><a href="client-side-development.html#servers-and-hosting"><i class="fa fa-check"></i><b>2.3</b> Servers and Hosting</a>
<ul>
<li class="chapter" data-level="" data-path="client-side-development.html"><a href="client-side-development.html#hosting-with-github-pages"><i class="fa fa-check"></i>Hosting with GitHub Pages</a></li>
<li class="chapter" data-level="" data-path="client-side-development.html"><a href="client-side-development.html#development-servers"><i class="fa fa-check"></i>Development Servers</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="client-side-development.html"><a href="client-side-development.html#web-standards"><i class="fa fa-check"></i><b>2.4</b> Web Standards</a></li>
<li class="chapter" data-level="2.5" data-path="client-side-development.html"><a href="client-side-development.html#web-accessibility"><i class="fa fa-check"></i><b>2.5</b> Web Accessibility</a>
<ul>
<li class="chapter" data-level="" data-path="client-side-development.html"><a href="client-side-development.html#supporting-accessibility"><i class="fa fa-check"></i>Supporting Accessibility</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="html-fundamentals.html"><a href="html-fundamentals.html"><i class="fa fa-check"></i><b>3</b> HTML Fundamentals</a>
<ul>
<li class="chapter" data-level="3.1" data-path="html-fundamentals.html"><a href="html-fundamentals.html#html-elements"><i class="fa fa-check"></i><b>3.1</b> HTML Elements</a>
<ul>
<li class="chapter" data-level="" data-path="html-fundamentals.html"><a href="html-fundamentals.html#some-example-elements"><i class="fa fa-check"></i>Some Example Elements</a></li>
<li class="chapter" data-level="" data-path="html-fundamentals.html"><a href="html-fundamentals.html#comments"><i class="fa fa-check"></i>Comments</a></li>
<li class="chapter" data-level="" data-path="html-fundamentals.html"><a href="html-fundamentals.html#attributes"><i class="fa fa-check"></i>Attributes</a></li>
<li class="chapter" data-level="" data-path="html-fundamentals.html"><a href="html-fundamentals.html#empty-elements"><i class="fa fa-check"></i>Empty Elements</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="html-fundamentals.html"><a href="html-fundamentals.html#nesting-elements"><i class="fa fa-check"></i><b>3.2</b> Nesting Elements</a>
<ul>
<li class="chapter" data-level="" data-path="html-fundamentals.html"><a href="html-fundamentals.html#block-vs.-inline-elements"><i class="fa fa-check"></i>Block vs. Inline Elements</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="html-fundamentals.html"><a href="html-fundamentals.html#web-page-structure"><i class="fa fa-check"></i><b>3.3</b> Web Page Structure</a></li>
<li class="chapter" data-level="" data-path="html-fundamentals.html"><a href="html-fundamentals.html#resources-1"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="css-fundamentals.html"><a href="css-fundamentals.html"><i class="fa fa-check"></i><b>4</b> CSS Fundamentals</a>
<ul>
<li class="chapter" data-level="4.1" data-path="css-fundamentals.html"><a href="css-fundamentals.html#why-two-different-languages"><i class="fa fa-check"></i><b>4.1</b> Why Two Different Languages?</a></li>
<li class="chapter" data-level="4.2" data-path="css-fundamentals.html"><a href="css-fundamentals.html#including-css"><i class="fa fa-check"></i><b>4.2</b> Including CSS</a></li>
<li class="chapter" data-level="4.3" data-path="css-fundamentals.html"><a href="css-fundamentals.html#css-rules"><i class="fa fa-check"></i><b>4.3</b> CSS Rules</a>
<ul>
<li class="chapter" data-level="" data-path="css-fundamentals.html"><a href="css-fundamentals.html#css-selector-basics"><i class="fa fa-check"></i>CSS Selector Basics</a></li>
<li class="chapter" data-level="" data-path="css-fundamentals.html"><a href="css-fundamentals.html#css-property-basics"><i class="fa fa-check"></i>CSS Property Basics</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="css-fundamentals.html"><a href="css-fundamentals.html#the-cascade"><i class="fa fa-check"></i><b>4.4</b> The Cascade</a></li>
<li class="chapter" data-level="" data-path="css-fundamentals.html"><a href="css-fundamentals.html#resources-2"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="semantic-html.html"><a href="semantic-html.html"><i class="fa fa-check"></i><b>5</b> Semantic HTML</a>
<ul>
<li class="chapter" data-level="5.1" data-path="semantic-html.html"><a href="semantic-html.html#specific-html-elements"><i class="fa fa-check"></i><b>5.1</b> Specific HTML Elements</a>
<ul>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#hyperlinks"><i class="fa fa-check"></i>Hyperlinks</a></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#inline-textual-elements"><i class="fa fa-check"></i>Inline Textual Elements</a></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#images-and-media"><i class="fa fa-check"></i>Images and Media</a></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#headings"><i class="fa fa-check"></i>Headings</a></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#non-semantic-elements"><i class="fa fa-check"></i>Non-Semantic Elements</a></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#sectioning-elements"><i class="fa fa-check"></i>Sectioning Elements</a></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#tables"><i class="fa fa-check"></i>Tables</a></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#forms"><i class="fa fa-check"></i>Forms</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="semantic-html.html"><a href="semantic-html.html#aria-for-accessibility"><i class="fa fa-check"></i><b>5.2</b> ARIA for Accessibility</a>
<ul>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#aria-labeling"><i class="fa fa-check"></i>ARIA Labeling</a></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#aria-roles-and-landmarks"><i class="fa fa-check"></i>ARIA Roles and Landmarks</a></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#aria-and-interactivity"><i class="fa fa-check"></i>ARIA and Interactivity</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="semantic-html.html"><a href="semantic-html.html#resources-3"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="css-selectors.html"><a href="css-selectors.html"><i class="fa fa-check"></i><b>6</b> CSS Selectors</a>
<ul>
<li class="chapter" data-level="6.1" data-path="css-selectors.html"><a href="css-selectors.html#basic-selectors"><i class="fa fa-check"></i><b>6.1</b> Basic Selectors</a>
<ul>
<li class="chapter" data-level="" data-path="css-selectors.html"><a href="css-selectors.html#type-selector"><i class="fa fa-check"></i>Type Selector</a></li>
<li class="chapter" data-level="" data-path="css-selectors.html"><a href="css-selectors.html#class-selector"><i class="fa fa-check"></i>Class Selector</a></li>
<li class="chapter" data-level="" data-path="css-selectors.html"><a href="css-selectors.html#id-selector"><i class="fa fa-check"></i>Id Selector</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="css-selectors.html"><a href="css-selectors.html#complex-selectors"><i class="fa fa-check"></i><b>6.2</b> Complex Selectors</a>
<ul>
<li class="chapter" data-level="" data-path="css-selectors.html"><a href="css-selectors.html#grouping-selector"><i class="fa fa-check"></i>Grouping Selector</a></li>
<li class="chapter" data-level="" data-path="css-selectors.html"><a href="css-selectors.html#compound-selector"><i class="fa fa-check"></i>Compound Selector</a></li>
<li class="chapter" data-level="" data-path="css-selectors.html"><a href="css-selectors.html#descendant-selector"><i class="fa fa-check"></i>Descendant Selector</a></li>
<li class="chapter" data-level="" data-path="css-selectors.html"><a href="css-selectors.html#pseudo-classes"><i class="fa fa-check"></i>Pseudo-classes</a></li>
<li class="chapter" data-level="" data-path="css-selectors.html"><a href="css-selectors.html#additional-selectors"><i class="fa fa-check"></i>Additional Selectors</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="css-selectors.html"><a href="css-selectors.html#selector-specificity"><i class="fa fa-check"></i><b>6.3</b> Selector Specificity</a></li>
<li class="chapter" data-level="" data-path="css-selectors.html"><a href="css-selectors.html#resources-4"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="css-properties.html"><a href="css-properties.html"><i class="fa fa-check"></i><b>7</b> CSS Properties</a>
<ul>
<li class="chapter" data-level="7.1" data-path="css-properties.html"><a href="css-properties.html#specifying-property-values"><i class="fa fa-check"></i><b>7.1</b> Specifying Property Values</a>
<ul>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#inherited-property-values"><i class="fa fa-check"></i>Inherited Property Values</a></li>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#css-units"><i class="fa fa-check"></i>Length Units & Sizes</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="css-properties.html"><a href="css-properties.html#fonts-and-text"><i class="fa fa-check"></i><b>7.2</b> Fonts and Text</a></li>
<li class="chapter" data-level="7.3" data-path="css-properties.html"><a href="css-properties.html#css-colors"><i class="fa fa-check"></i><b>7.3</b> Colors and Backgrounds</a>
<ul>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#backgrounds-and-images"><i class="fa fa-check"></i>Backgrounds and Images</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="css-properties.html"><a href="css-properties.html#box-model"><i class="fa fa-check"></i><b>7.4</b> Spacing with the Box Model</a>
<ul>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#padding"><i class="fa fa-check"></i>Padding</a></li>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#border"><i class="fa fa-check"></i>Border</a></li>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#margin"><i class="fa fa-check"></i>Margin</a></li>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#padding-vs.-margin"><i class="fa fa-check"></i>Padding vs. Margin</a></li>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#box-sizing"><i class="fa fa-check"></i>Box-Sizing</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="css-properties.html"><a href="css-properties.html#flow-layout"><i class="fa fa-check"></i><b>7.5</b> Flow Layout</a></li>
<li class="chapter" data-level="7.6" data-path="css-properties.html"><a href="css-properties.html#alternate-positioning"><i class="fa fa-check"></i><b>7.6</b> Alternate Positioning</a>
<ul>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#floating"><i class="fa fa-check"></i>Floating</a></li>
</ul></li>
<li class="chapter" data-level="7.7" data-path="css-properties.html"><a href="css-properties.html#flexbox"><i class="fa fa-check"></i><b>7.7</b> Flexbox</a></li>
<li class="chapter" data-level="" data-path="css-properties.html"><a href="css-properties.html#resources-5"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="responsive-css.html"><a href="responsive-css.html"><i class="fa fa-check"></i><b>8</b> Responsive CSS</a>
<ul>
<li class="chapter" data-level="8.1" data-path="responsive-css.html"><a href="responsive-css.html#mobile-first-design"><i class="fa fa-check"></i><b>8.1</b> Mobile-First Design</a>
<ul>
<li class="chapter" data-level="" data-path="responsive-css.html"><a href="responsive-css.html#mobile-first-design-principles"><i class="fa fa-check"></i>Mobile-First Design Principles</a></li>
<li class="chapter" data-level="" data-path="responsive-css.html"><a href="responsive-css.html#specifying-viewport"><i class="fa fa-check"></i>Specifying Viewport</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="responsive-css.html"><a href="responsive-css.html#media-queries"><i class="fa fa-check"></i><b>8.2</b> Media Queries</a>
<ul>
<li class="chapter" data-level="" data-path="responsive-css.html"><a href="responsive-css.html#example-responsive-flexbox"><i class="fa fa-check"></i>Example: Responsive Flexbox</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="responsive-css.html"><a href="responsive-css.html#resources-6"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="css-frameworks.html"><a href="css-frameworks.html"><i class="fa fa-check"></i><b>9</b> CSS Frameworks</a>
<ul>
<li class="chapter" data-level="9.1" data-path="css-frameworks.html"><a href="css-frameworks.html#using-a-framework"><i class="fa fa-check"></i><b>9.1</b> Using a Framework</a>
<ul>
<li class="chapter" data-level="" data-path="css-frameworks.html"><a href="css-frameworks.html#including-a-framework"><i class="fa fa-check"></i>Including a Framework</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="css-frameworks.html"><a href="css-frameworks.html#bootstrap"><i class="fa fa-check"></i><b>9.2</b> Bootstrap</a>
<ul>
<li class="chapter" data-level="" data-path="css-frameworks.html"><a href="css-frameworks.html#utility-classes"><i class="fa fa-check"></i>Utility Classes</a></li>
<li class="chapter" data-level="" data-path="css-frameworks.html"><a href="css-frameworks.html#responsive-design"><i class="fa fa-check"></i>Responsive Design</a></li>
<li class="chapter" data-level="" data-path="css-frameworks.html"><a href="css-frameworks.html#the-grid"><i class="fa fa-check"></i>The Grid</a></li>
<li class="chapter" data-level="" data-path="css-frameworks.html"><a href="css-frameworks.html#components"><i class="fa fa-check"></i>Components</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="css-frameworks.html"><a href="css-frameworks.html#resources-7"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="javascript.html"><a href="javascript.html"><i class="fa fa-check"></i><b>10</b> JavaScript Fundamentals</a>
<ul>
<li class="chapter" data-level="10.1" data-path="javascript.html"><a href="javascript.html#programming-with-javascript"><i class="fa fa-check"></i><b>10.1</b> Programming with JavaScript</a>
<ul>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#history-and-versions"><i class="fa fa-check"></i>History and Versions</a></li>
</ul></li>
<li class="chapter" data-level="10.2" data-path="javascript.html"><a href="javascript.html#running-javascript"><i class="fa fa-check"></i><b>10.2</b> Running JavaScript</a>
<ul>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#in-the-browser"><i class="fa fa-check"></i>In the Browser</a></li>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#on-the-command-line-with-node.js"><i class="fa fa-check"></i>On the Command-line with Node.js</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="javascript.html"><a href="javascript.html#writing-scripts"><i class="fa fa-check"></i><b>10.3</b> Writing Scripts</a>
<ul>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#strict-mode"><i class="fa fa-check"></i>Strict Mode</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="javascript.html"><a href="javascript.html#variables"><i class="fa fa-check"></i><b>10.4</b> Variables</a>
<ul>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#basic-data-types"><i class="fa fa-check"></i>Basic Data Types</a></li>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#arrays"><i class="fa fa-check"></i>Arrays</a></li>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#objects"><i class="fa fa-check"></i>Objects</a></li>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#type-coercion"><i class="fa fa-check"></i>Type Coercion</a></li>
</ul></li>
<li class="chapter" data-level="10.5" data-path="javascript.html"><a href="javascript.html#control-structures"><i class="fa fa-check"></i><b>10.5</b> Control Structures</a></li>
<li class="chapter" data-level="10.6" data-path="javascript.html"><a href="javascript.html#functions"><i class="fa fa-check"></i><b>10.6</b> Functions</a></li>
<li class="chapter" data-level="" data-path="javascript.html"><a href="javascript.html#resources-8"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="functional-programming.html"><a href="functional-programming.html"><i class="fa fa-check"></i><b>11</b> Functional Programming in JS</a>
<ul>
<li class="chapter" data-level="11.1" data-path="functional-programming.html"><a href="functional-programming.html#functions-are-variables"><i class="fa fa-check"></i><b>11.1</b> Functions ARE Variables</a>
<ul>
<li class="chapter" data-level="" data-path="functional-programming.html"><a href="functional-programming.html#anonymous-functions"><i class="fa fa-check"></i>Anonymous Functions</a></li>
</ul></li>
<li class="chapter" data-level="11.2" data-path="functional-programming.html"><a href="functional-programming.html#object-functions"><i class="fa fa-check"></i><b>11.2</b> Object Functions</a></li>
<li class="chapter" data-level="11.3" data-path="functional-programming.html"><a href="functional-programming.html#callback-functions"><i class="fa fa-check"></i><b>11.3</b> Callback Functions</a>
<ul>
<li class="chapter" data-level="" data-path="functional-programming.html"><a href="functional-programming.html#closures"><i class="fa fa-check"></i>Closures</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="functional-programming.html"><a href="functional-programming.html#functional-looping"><i class="fa fa-check"></i><b>11.4</b> Functional Looping</a>
<ul>
<li class="chapter" data-level="" data-path="functional-programming.html"><a href="functional-programming.html#map"><i class="fa fa-check"></i>Map</a></li>
<li class="chapter" data-level="" data-path="functional-programming.html"><a href="functional-programming.html#filter"><i class="fa fa-check"></i>Filter</a></li>
<li class="chapter" data-level="" data-path="functional-programming.html"><a href="functional-programming.html#reduce"><i class="fa fa-check"></i>Reduce</a></li>
</ul></li>
<li class="chapter" data-level="11.5" data-path="functional-programming.html"><a href="functional-programming.html#pure-functions"><i class="fa fa-check"></i><b>11.5</b> Pure Functions</a></li>
<li class="chapter" data-level="" data-path="functional-programming.html"><a href="functional-programming.html#resources-9"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="dom.html"><a href="dom.html"><i class="fa fa-check"></i><b>12</b> Document Object Model (DOM)</a>
<ul>
<li class="chapter" data-level="12.1" data-path="dom.html"><a href="dom.html#the-dom-api"><i class="fa fa-check"></i><b>12.1</b> The DOM API</a>
<ul>
<li class="chapter" data-level="" data-path="dom.html"><a href="dom.html#global-variables"><i class="fa fa-check"></i>Global Variables</a></li>
</ul></li>
<li class="chapter" data-level="12.2" data-path="dom.html"><a href="dom.html#dom-manipulation"><i class="fa fa-check"></i><b>12.2</b> DOM Manipulation</a>
<ul>
<li class="chapter" data-level="" data-path="dom.html"><a href="dom.html#referencing-html-elements"><i class="fa fa-check"></i>Referencing HTML Elements</a></li>
<li class="chapter" data-level="" data-path="dom.html"><a href="dom.html#modifying-html-elements"><i class="fa fa-check"></i>Modifying HTML Elements</a></li>
<li class="chapter" data-level="" data-path="dom.html"><a href="dom.html#modifying-the-dom-tree"><i class="fa fa-check"></i>Modifying the DOM Tree</a></li>
<li class="chapter" data-level="" data-path="dom.html"><a href="dom.html#accessibility"><i class="fa fa-check"></i>Accessibility</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="dom.html"><a href="dom.html#listening-for-events"><i class="fa fa-check"></i><b>12.3</b> Listening for Events</a>
<ul>
<li class="chapter" data-level="" data-path="dom.html"><a href="dom.html#types-of-events"><i class="fa fa-check"></i>Types of Events</a></li>
<li class="chapter" data-level="" data-path="dom.html"><a href="dom.html#event-driven-programming"><i class="fa fa-check"></i>Event-Driven Programming</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="dom.html"><a href="dom.html#resources-10"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="javascript-libraries.html"><a href="javascript-libraries.html"><i class="fa fa-check"></i><b>13</b> JavaScript Libraries</a>
<ul>
<li class="chapter" data-level="13.1" data-path="javascript-libraries.html"><a href="javascript-libraries.html#including-a-library"><i class="fa fa-check"></i><b>13.1</b> Including a Library</a></li>
<li class="chapter" data-level="13.2" data-path="javascript-libraries.html"><a href="javascript-libraries.html#example-jquery"><i class="fa fa-check"></i><b>13.2</b> Example: jQuery</a>
<ul>
<li class="chapter" data-level="" data-path="javascript-libraries.html"><a href="javascript-libraries.html#maniputing-the-dom"><i class="fa fa-check"></i>Maniputing the DOM</a></li>
<li class="chapter" data-level="" data-path="javascript-libraries.html"><a href="javascript-libraries.html#handling-events"><i class="fa fa-check"></i>Handling Events</a></li>
<li class="chapter" data-level="" data-path="javascript-libraries.html"><a href="javascript-libraries.html#and-more"><i class="fa fa-check"></i>And more!</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="javascript-libraries.html"><a href="javascript-libraries.html#resources-11"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="es6.html"><a href="es6.html"><i class="fa fa-check"></i><b>14</b> ES6+ Syntax</a>
<ul>
<li class="chapter" data-level="14.1" data-path="es6.html"><a href="es6.html#es6-syntax-features"><i class="fa fa-check"></i><b>14.1</b> ES6+ Syntax Features</a>
<ul>
<li class="chapter" data-level="" data-path="es6.html"><a href="es6.html#arrow-functions"><i class="fa fa-check"></i>Arrow Functions</a></li>
<li class="chapter" data-level="" data-path="es6.html"><a href="es6.html#destructuring"><i class="fa fa-check"></i>Destructuring</a></li>
<li class="chapter" data-level="" data-path="es6.html"><a href="es6.html#spreading"><i class="fa fa-check"></i>Spreading</a></li>
<li class="chapter" data-level="" data-path="es6.html"><a href="es6.html#template-strings"><i class="fa fa-check"></i>Template Strings</a></li>
</ul></li>
<li class="chapter" data-level="14.2" data-path="es6.html"><a href="es6.html#modules"><i class="fa fa-check"></i><b>14.2</b> Modules</a>
<ul>
<li class="chapter" data-level="" data-path="es6.html"><a href="es6.html#module-syntax"><i class="fa fa-check"></i>Module Syntax</a></li>
</ul></li>
<li class="chapter" data-level="14.3" data-path="es6.html"><a href="es6.html#es6-classes"><i class="fa fa-check"></i><b>14.3</b> Classes</a>
<ul>
<li class="chapter" data-level="" data-path="es6.html"><a href="es6.html#why-classes"><i class="fa fa-check"></i>Why Classes?</a></li>
<li class="chapter" data-level="" data-path="es6.html"><a href="es6.html#review-classes-in-java"><i class="fa fa-check"></i>Review: Classes in Java</a></li>
<li class="chapter" data-level="" data-path="es6.html"><a href="es6.html#es6-class-syntax"><i class="fa fa-check"></i>ES6 Class Syntax</a></li>
<li class="chapter" data-level="" data-path="es6.html"><a href="es6.html#inheritance"><i class="fa fa-check"></i>Inheritance</a></li>
<li><a href="es6.html#working-with-this">Working with <code>this</code></a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="15" data-path="react.html"><a href="react.html"><i class="fa fa-check"></i><b>15</b> Introduction to React</a>
<ul>
<li class="chapter" data-level="15.1" data-path="react.html"><a href="react.html#react-vite-setup"><i class="fa fa-check"></i><b>15.1</b> Getting Set Up: React and Vite</a>
<ul>
<li class="chapter" data-level="" data-path="react.html"><a href="react.html#running-the-development-server"><i class="fa fa-check"></i>Running the Development Server</a></li>
<li class="chapter" data-level="" data-path="react.html"><a href="react.html#project-structure"><i class="fa fa-check"></i>Project Structure</a></li>
</ul></li>
<li class="chapter" data-level="15.2" data-path="react.html"><a href="react.html#jsx"><i class="fa fa-check"></i><b>15.2</b> JSX</a>
<ul>
<li class="chapter" data-level="" data-path="react.html"><a href="react.html#inline-expressions"><i class="fa fa-check"></i>Inline Expressions</a></li>
</ul></li>
<li class="chapter" data-level="15.3" data-path="react.html"><a href="react.html#react-components"><i class="fa fa-check"></i><b>15.3</b> React Components</a>
<ul>
<li class="chapter" data-level="" data-path="react.html"><a href="react.html#component-organization"><i class="fa fa-check"></i>Component Organization</a></li>
<li class="chapter" data-level="" data-path="react.html"><a href="react.html#deprecated-alternative-class-components"><i class="fa fa-check"></i>Deprecated Alternative: Class Components</a></li>
</ul></li>
<li class="chapter" data-level="15.4" data-path="react.html"><a href="react.html#props"><i class="fa fa-check"></i><b>15.4</b> Properties (<code>props</code>)</a>
<ul>
<li class="chapter" data-level="" data-path="react.html"><a href="react.html#props-and-composition"><i class="fa fa-check"></i>Props and Composition</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="react.html"><a href="react.html#resources-12"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="16" data-path="interactive-react.html"><a href="interactive-react.html"><i class="fa fa-check"></i><b>16</b> Interactive React</a>
<ul>
<li class="chapter" data-level="16.1" data-path="interactive-react.html"><a href="interactive-react.html#handling-events-in-react"><i class="fa fa-check"></i><b>16.1</b> Handling Events in React</a></li>
<li class="chapter" data-level="16.2" data-path="interactive-react.html"><a href="interactive-react.html#state-and-hooks"><i class="fa fa-check"></i><b>16.2</b> State and Hooks</a>
<ul>
<li class="chapter" data-level="" data-path="interactive-react.html"><a href="interactive-react.html#updating-state"><i class="fa fa-check"></i>Updating State</a></li>
<li class="chapter" data-level="" data-path="interactive-react.html"><a href="interactive-react.html#state-vs.-props"><i class="fa fa-check"></i>State vs. Props</a></li>
<li class="chapter" data-level="" data-path="interactive-react.html"><a href="interactive-react.html#lifting-up-state"><i class="fa fa-check"></i>Lifting Up State</a></li>
</ul></li>
<li class="chapter" data-level="16.3" data-path="interactive-react.html"><a href="interactive-react.html#specific-interactions"><i class="fa fa-check"></i><b>16.3</b> Specific Interactions</a>
<ul>
<li class="chapter" data-level="" data-path="interactive-react.html"><a href="interactive-react.html#working-with-forms"><i class="fa fa-check"></i>Working with Forms</a></li>
<li class="chapter" data-level="" data-path="interactive-react.html"><a href="interactive-react.html#fetching-data-via-ajax"><i class="fa fa-check"></i>Fetching Data via AJAX</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="interactive-react.html"><a href="interactive-react.html#resources-13"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="17" data-path="client-side-routing.html"><a href="client-side-routing.html"><i class="fa fa-check"></i><b>17</b> Client-Side Routing</a>
<ul>
<li class="chapter" data-level="17.1" data-path="client-side-routing.html"><a href="client-side-routing.html#single-page-applications"><i class="fa fa-check"></i><b>17.1</b> Single-Page Applications</a></li>
<li class="chapter" data-level="17.2" data-path="client-side-routing.html"><a href="client-side-routing.html#react-router"><i class="fa fa-check"></i><b>17.2</b> React-Router</a>
<ul>
<li class="chapter" data-level="" data-path="client-side-routing.html"><a href="client-side-routing.html#routing"><i class="fa fa-check"></i>Routing</a></li>
<li class="chapter" data-level="" data-path="client-side-routing.html"><a href="client-side-routing.html#linking"><i class="fa fa-check"></i>Linking</a></li>
<li class="chapter" data-level="" data-path="client-side-routing.html"><a href="client-side-routing.html#react-router-and-hosting"><i class="fa fa-check"></i>React Router and Hosting</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="client-side-routing.html"><a href="client-side-routing.html#resources-14"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="ajax.html"><a href="ajax.html"><i class="fa fa-check"></i><b>18</b> AJAX Requests</a>
<ul>
<li class="chapter" data-level="18.1" data-path="ajax.html"><a href="ajax.html#ajax-1"><i class="fa fa-check"></i><b>18.1</b> AJAX</a>
<ul>
<li class="chapter" data-level="" data-path="ajax.html"><a href="ajax.html#xml-and-json"><i class="fa fa-check"></i>XML and JSON</a></li>
</ul></li>
<li class="chapter" data-level="18.2" data-path="ajax.html"><a href="ajax.html#fetching-data"><i class="fa fa-check"></i><b>18.2</b> Fetching Data</a></li>
<li class="chapter" data-level="18.3" data-path="ajax.html"><a href="ajax.html#asynchronous-programming"><i class="fa fa-check"></i><b>18.3</b> Asynchronous Programming</a>
<ul>
<li class="chapter" data-level="" data-path="ajax.html"><a href="ajax.html#chaining-promises"><i class="fa fa-check"></i>Chaining Promises</a></li>
<li class="chapter" data-level="" data-path="ajax.html"><a href="ajax.html#handling-errors"><i class="fa fa-check"></i>Handling Errors</a></li>
<li class="chapter" data-level="" data-path="ajax.html"><a href="ajax.html#other-data-formats"><i class="fa fa-check"></i>Other Data Formats</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="ajax.html"><a href="ajax.html#resources-15"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="19" data-path="firebase.html"><a href="firebase.html"><i class="fa fa-check"></i><b>19</b> Firebase</a>
<ul>
<li class="chapter" data-level="19.1" data-path="firebase.html"><a href="firebase.html#setting-up-firebase"><i class="fa fa-check"></i><b>19.1</b> Setting up Firebase</a>
<ul>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#creating-a-project"><i class="fa fa-check"></i>Creating a Project</a></li>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#including-firebase-in-react"><i class="fa fa-check"></i>Including Firebase in React</a></li>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#adding-collaborators-to-a-project"><i class="fa fa-check"></i>Adding Collaborators to a Project</a></li>
</ul></li>
<li class="chapter" data-level="19.2" data-path="firebase.html"><a href="firebase.html#realtime-database"><i class="fa fa-check"></i><b>19.2</b> Realtime Database</a>
<ul>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#setting-up-the-database"><i class="fa fa-check"></i>Setting Up the Database</a></li>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#data-references"><i class="fa fa-check"></i>Data References</a></li>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#writing-data"><i class="fa fa-check"></i>Writing Data</a></li>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#listening-for-data-changes"><i class="fa fa-check"></i>Listening for Data Changes</a></li>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#firebase-arrays"><i class="fa fa-check"></i>Firebase Arrays</a></li>
</ul></li>
<li class="chapter" data-level="19.3" data-path="firebase.html"><a href="firebase.html#user-authentication"><i class="fa fa-check"></i><b>19.3</b> User Authentication</a>
<ul>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#signing-in-with-firebaseui"><i class="fa fa-check"></i>Signing In with FirebaseUI</a></li>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#managing-the-user"><i class="fa fa-check"></i>Managing the User</a></li>
</ul></li>
<li class="chapter" data-level="19.4" data-path="firebase.html"><a href="firebase.html#firebase-storage"><i class="fa fa-check"></i><b>19.4</b> Firebase Storage</a>
<ul>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#file-inputs"><i class="fa fa-check"></i>File Inputs</a></li>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#uploading-files"><i class="fa fa-check"></i>Uploading Files</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="firebase.html"><a href="firebase.html#resources-16"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="appendix"><span><b>Appendices & Special Topics</b></span></li>
<li class="chapter" data-level="A" data-path="code-style-guide.html"><a href="code-style-guide.html"><i class="fa fa-check"></i><b>A</b> Code Style Guide</a>
<ul>
<li class="chapter" data-level="A.1" data-path="code-style-guide.html"><a href="code-style-guide.html#html-guidelines"><i class="fa fa-check"></i><b>A.1</b> HTML Guidelines</a>
<ul>
<li class="chapter" data-level="" data-path="code-style-guide.html"><a href="code-style-guide.html#spacing"><i class="fa fa-check"></i>Spacing</a></li>
<li class="chapter" data-level="" data-path="code-style-guide.html"><a href="code-style-guide.html#specific-elements"><i class="fa fa-check"></i>Specific Elements</a></li>
<li class="chapter" data-level="" data-path="code-style-guide.html"><a href="code-style-guide.html#comments-in-html"><i class="fa fa-check"></i>Comments in HTML</a></li>
</ul></li>
<li class="chapter" data-level="A.2" data-path="code-style-guide.html"><a href="code-style-guide.html#css-guidelines"><i class="fa fa-check"></i><b>A.2</b> CSS Guidelines</a>
<ul>
<li class="chapter" data-level="A.2.1" data-path="code-style-guide.html"><a href="code-style-guide.html#spacing-1"><i class="fa fa-check"></i><b>A.2.1</b> Spacing</a></li>
<li class="chapter" data-level="A.2.2" data-path="code-style-guide.html"><a href="code-style-guide.html#selectors"><i class="fa fa-check"></i><b>A.2.2</b> Selectors</a></li>
<li class="chapter" data-level="A.2.3" data-path="code-style-guide.html"><a href="code-style-guide.html#class-names"><i class="fa fa-check"></i><b>A.2.3</b> Class Names</a></li>
<li class="chapter" data-level="A.2.4" data-path="code-style-guide.html"><a href="code-style-guide.html#specific-properties"><i class="fa fa-check"></i><b>A.2.4</b> Specific Properties</a></li>
<li class="chapter" data-level="A.2.5" data-path="code-style-guide.html"><a href="code-style-guide.html#responsive-css-1"><i class="fa fa-check"></i><b>A.2.5</b> Responsive CSS</a></li>
<li class="chapter" data-level="A.2.6" data-path="code-style-guide.html"><a href="code-style-guide.html#comments-in-css"><i class="fa fa-check"></i><b>A.2.6</b> Comments in CSS</a></li>
</ul></li>
<li class="chapter" data-level="A.3" data-path="code-style-guide.html"><a href="code-style-guide.html#javascript-guidelines"><i class="fa fa-check"></i><b>A.3</b> JavaScript Guidelines</a>
<ul>
<li class="chapter" data-level="A.3.1" data-path="code-style-guide.html"><a href="code-style-guide.html#variables-1"><i class="fa fa-check"></i><b>A.3.1</b> Variables</a></li>
<li class="chapter" data-level="A.3.2" data-path="code-style-guide.html"><a href="code-style-guide.html#functions-1"><i class="fa fa-check"></i><b>A.3.2</b> Functions</a></li>
<li class="chapter" data-level="A.3.3" data-path="code-style-guide.html"><a href="code-style-guide.html#comments-in-javascript"><i class="fa fa-check"></i><b>A.3.3</b> Comments in JavaScript</a></li>
<li class="chapter" data-level="A.3.4" data-path="code-style-guide.html"><a href="code-style-guide.html#miscellaneous-javascript-guidelines"><i class="fa fa-check"></i><b>A.3.4</b> Miscellaneous JavaScript Guidelines</a></li>
</ul></li>
<li class="chapter" data-level="A.4" data-path="code-style-guide.html"><a href="code-style-guide.html#react-guidelines"><i class="fa fa-check"></i><b>A.4</b> React Guidelines</a>
<ul>
<li class="chapter" data-level="A.4.1" data-path="code-style-guide.html"><a href="code-style-guide.html#components-1"><i class="fa fa-check"></i><b>A.4.1</b> Components</a></li>
<li class="chapter" data-level="A.4.2" data-path="code-style-guide.html"><a href="code-style-guide.html#mapping-data"><i class="fa fa-check"></i><b>A.4.2</b> Mapping Data</a></li>
<li class="chapter" data-level="A.4.3" data-path="code-style-guide.html"><a href="code-style-guide.html#state"><i class="fa fa-check"></i><b>A.4.3</b> State</a></li>
<li class="chapter" data-level="A.4.4" data-path="code-style-guide.html"><a href="code-style-guide.html#events-and-forms"><i class="fa fa-check"></i><b>A.4.4</b> Events and Forms</a></li>
</ul></li>
<li class="chapter" data-level="A.5" data-path="code-style-guide.html"><a href="code-style-guide.html#miscellaneous-guidelines"><i class="fa fa-check"></i><b>A.5</b> Miscellaneous Guidelines</a></li>
</ul></li>
<li class="chapter" data-level="B" data-path="jest.html"><a href="jest.html"><i class="fa fa-check"></i><b>B</b> Testing with Jest</a>
<ul>
<li class="chapter" data-level="B.1" data-path="jest.html"><a href="jest.html#testing"><i class="fa fa-check"></i><b>B.1</b> Testing</a></li>
<li class="chapter" data-level="B.2" data-path="jest.html"><a href="jest.html#testing-with-jest"><i class="fa fa-check"></i><b>B.2</b> Testing with Jest</a></li>
<li class="chapter" data-level="B.3" data-path="jest.html"><a href="jest.html#writing-a-test"><i class="fa fa-check"></i><b>B.3</b> Writing a Test</a>
<ul>
<li class="chapter" data-level="" data-path="jest.html"><a href="jest.html#assertions-and-matchers"><i class="fa fa-check"></i>Assertions and Matchers</a></li>
<li class="chapter" data-level="" data-path="jest.html"><a href="jest.html#organizing-tests"><i class="fa fa-check"></i>Organizing Tests</a></li>
<li class="chapter" data-level="" data-path="jest.html"><a href="jest.html#running-the-tests"><i class="fa fa-check"></i>Running the Tests</a></li>
<li class="chapter" data-level="" data-path="jest.html"><a href="jest.html#practice"><i class="fa fa-check"></i>Practice</a></li>
</ul></li>
<li class="chapter" data-level="B.4" data-path="jest.html"><a href="jest.html#testing-web-apps-with-jest"><i class="fa fa-check"></i><b>B.4</b> Testing Web Apps with Jest</a>
<ul>
<li class="chapter" data-level="" data-path="jest.html"><a href="jest.html#practice-1"><i class="fa fa-check"></i>Practice</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="C" data-path="webpack.html"><a href="webpack.html"><i class="fa fa-check"></i><b>C</b> Lab: Webpack</a>
<ul>
<li class="chapter" data-level="C.1" data-path="webpack.html"><a href="webpack.html#what-is-webpack"><i class="fa fa-check"></i><b>C.1</b> What is Webpack?</a></li>
<li class="chapter" data-level="C.2" data-path="webpack.html"><a href="webpack.html#getting-started"><i class="fa fa-check"></i><b>C.2</b> Getting Started</a></li>
<li class="chapter" data-level="C.3" data-path="webpack.html"><a href="webpack.html#webpack.config.js"><i class="fa fa-check"></i><b>C.3</b> <code>webpack.config.js</code></a>
<ul>
<li><a href="webpack.html#entry-and-output"><code>entry</code> and <code>output</code></a></li>
</ul></li>
<li class="chapter" data-level="C.4" data-path="webpack.html"><a href="webpack.html#loaders"><i class="fa fa-check"></i><b>C.4</b> Loaders</a>
<ul>
<li class="chapter" data-level="" data-path="webpack.html"><a href="webpack.html#babel-loader"><i class="fa fa-check"></i>Babel Loader</a></li>
<li class="chapter" data-level="" data-path="webpack.html"><a href="webpack.html#clean-up-bath"><i class="fa fa-check"></i>Clean up :bath:</a></li>
<li class="chapter" data-level="" data-path="webpack.html"><a href="webpack.html#further-loader-practice"><i class="fa fa-check"></i>Further Loader Practice</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="D" data-path="class-components.html"><a href="class-components.html"><i class="fa fa-check"></i><b>D</b> React Class Components</a>
<ul>
<li class="chapter" data-level="D.1" data-path="class-components.html"><a href="class-components.html#props-in-class-components"><i class="fa fa-check"></i><b>D.1</b> Props in Class Components</a></li>
<li class="chapter" data-level="D.2" data-path="class-components.html"><a href="class-components.html#handling-events-in-class-components"><i class="fa fa-check"></i><b>D.2</b> Handling Events in Class Components</a>
<ul>
<li><a href="class-components.html#accessing-this-component-from-events">Accessing <code>this</code> Component from Events</a></li>
</ul></li>
<li class="chapter" data-level="D.3" data-path="class-components.html"><a href="class-components.html#state-in-class-components"><i class="fa fa-check"></i><b>D.3</b> State in Class Components</a>
<ul>
<li class="chapter" data-level="" data-path="class-components.html"><a href="class-components.html#changing-the-state"><i class="fa fa-check"></i>Changing the State</a></li>
</ul></li>
<li class="chapter" data-level="D.4" data-path="class-components.html"><a href="class-components.html#the-component-lifecycle"><i class="fa fa-check"></i><b>D.4</b> The Component Lifecycle</a>
<ul>
<li class="chapter" data-level="" data-path="class-components.html"><a href="class-components.html#lifecycle-example-fetching-data-via-ajax"><i class="fa fa-check"></i>Lifecycle Example: Fetching Data via AJAX</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="class-components.html"><a href="class-components.html#resources-17"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="E" data-path="css-in-js.html"><a href="css-in-js.html"><i class="fa fa-check"></i><b>E</b> CSS in JS</a>
<ul>
<li class="chapter" data-level="E.1" data-path="css-in-js.html"><a href="css-in-js.html#why-css-in-js"><i class="fa fa-check"></i><b>E.1</b> Why CSS in JS?</a></li>
<li class="chapter" data-level="E.2" data-path="css-in-js.html"><a href="css-in-js.html#react-inline-styles"><i class="fa fa-check"></i><b>E.2</b> React Inline Styles</a></li>
<li class="chapter" data-level="E.3" data-path="css-in-js.html"><a href="css-in-js.html#aphrodite"><i class="fa fa-check"></i><b>E.3</b> Aphrodite</a></li>
<li class="chapter" data-level="E.4" data-path="css-in-js.html"><a href="css-in-js.html#css-modules"><i class="fa fa-check"></i><b>E.4</b> CSS Modules</a>
<ul>
<li class="chapter" data-level="" data-path="css-in-js.html"><a href="css-in-js.html#ejecting-from-create-react-app"><i class="fa fa-check"></i>Ejecting from Create React App</a></li>
<li class="chapter" data-level="" data-path="css-in-js.html"><a href="css-in-js.html#composing-classes"><i class="fa fa-check"></i>Composing Classes</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="css-in-js.html"><a href="css-in-js.html#resources-18"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="F" data-path="redux.html"><a href="redux.html"><i class="fa fa-check"></i><b>F</b> Redux</a></li>
<li class="chapter" data-level="G" data-path="react-native.html"><a href="react-native.html"><i class="fa fa-check"></i><b>G</b> React Native</a>
<ul>
<li class="chapter" data-level="G.1" data-path="react-native.html"><a href="react-native.html#getting-setup"><i class="fa fa-check"></i><b>G.1</b> Getting Setup</a>
<ul>
<li class="chapter" data-level="" data-path="react-native.html"><a href="react-native.html#running-react-native-apps"><i class="fa fa-check"></i>Running React Native Apps</a></li>
</ul></li>
<li class="chapter" data-level="G.2" data-path="react-native.html"><a href="react-native.html#react-native-apps"><i class="fa fa-check"></i><b>G.2</b> React Native Apps</a>
<ul>
<li class="chapter" data-level="" data-path="react-native.html"><a href="react-native.html#styling-react-native"><i class="fa fa-check"></i>Styling React Native</a></li>
<li class="chapter" data-level="" data-path="react-native.html"><a href="react-native.html#interaction"><i class="fa fa-check"></i>Interaction</a></li>
<li class="chapter" data-level="" data-path="react-native.html"><a href="react-native.html#lists-and-data"><i class="fa fa-check"></i>Lists and Data</a></li>
</ul></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="./">Client-Side Web Development</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="10">
<h1><span class="header-section-number">Chapter 10</span> JavaScript Fundamentals</h1>
<p>HTML and CSS are <em>markup languages</em>, used to annotate websites to describe their meaning and appearance. But they are not “full” (i.e., <a href="https://en.wikipedia.org/wiki/Turing_completeness">Turing Complete</a>) programming languages—they don’t have variables, control structures, or other features required for the computer to execute an <em>algorithm</em>. So to make <em>interactive</em> websites or complex web applications, you need a complete programming language. And the language used by browsers is <strong>JavaScript</strong>. This chapter introduces the fundamentals of the JavaScript language as used in web development, focusing on variables, data types, and basic control structures. Functions are introduced, but addressed in more detail in the next chapter.</p>
<p class="alert alert-warning">
Note, this course assumes you already are already familiar with introductory programming in a imperative language such as Java. It focuses on how JavaScript differs from languages such as Java, rather than foundational programming concepts. If you need review on those topics, you can check out a <a href="https://en.wikiversity.org/wiki/Introduction_to_Programming">basic programming tutorial</a> or review your notes from a previous programming course.
</p>
<div id="programming-with-javascript" class="section level2" number="10.1">
<h2><span class="header-section-number">10.1</span> Programming with JavaScript</h2>
<p>JavaScript is a <strong>high-level, general-purpose programming language</strong>, allowing you to declare instructions for the computer in an almost-human readable way (similar to Java). JavaScript is an <em>imperative</em> language, so you write algorithms as step-by-step instructions (lines of code) using JavaScript’s syntax, and the computer will interpret these instructions in order to execute the algorithm. Browsers are able to download scripts written in JavaScript, executing them line-by-line and using those instructions to manipulate what content is displayed.</p>
<p>Indeed, JavaScript is an <strong>interpreted language</strong>, in that the computer (specifically a <em>JavaScript Interpreter</em>) translates the high-level language into machine language <em>on the fly at runtime</em>. The interpreter will read and execute one line of code at a time, executing that line before it even begins to consider the next. This is in contrast with <em>compiled languages</em> like C or Java that have the computer do the translation in one pass (at compile-time), and then only execute the program after the whole thing is converted to machine language.</p>
<ul>
<li><p>This on-the-fly translation means that interpreted languages like JavaScript are usually slower than compiled languages, but not enough that we’ll notice (web browsers include <em>highly</em> optimized interpreters). The bigger drawback is that without the compile step, program errors appear at runtime rather than compile time, making them more difficult to catch and fix. As such, JavaScript developers make heavy use of various <a href="https://en.wikipedia.org/wiki/Lint_%28software%29">linting</a> tools (which flag common problems that can lead to runtime errors), as well as <a href="https://facebook.github.io/jest/">automated testing</a> systems to check against a variety of inputs.</p>
<p>Many JavaScript editors do this kind of error checking—for example, VS Code provides syntax error checking out of the box, with more <a href="https://code.visualstudio.com/docs/languages/javascript#_linters">detailed linting</a> available through extensions. See <a href="https://code.visualstudio.com/docs/languages/javascript">this article</a> for more code features that support programming in JavaScript, including IntelliSense and type checking.</p></li>
</ul>
<p>Although JavaScript was designed for and is most commonly used within web browsers (which contain their own JavaScript Interpreters), it can also be executed on the command line by using <a href="https://nodejs.org/en/">Node.js</a>, allowing JavaScript to be a fully general language. Both techniques are described in this chapter.</p>
<div id="history-and-versions" class="section level3 unnumbered">
<h3>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>
<ul>
<li><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. It is the programming equivalent of the <a href="https://www.rottentomatoes.com/m/transmorphers_2007/">Transmorphers</a> movie.</p></li>
<li><p>The JavaScript language is officially an implementation of <a href="https://en.wikipedia.org/wiki/ECMAScript">ECMAScript</a> (named for the European Computer Manufacturers Association) specification. Hence versions of JavaScript are labeled with the prefix “ES”. For example, this chapter describes <code>ES5</code> (JavaScript 5) syntax.</p></li>
</ul>
<p>Like HTML and CSS, the JavaScript language continues to be developed and refined through new versions. Each new version of JavaScript includes additional syntax shorts, specialized keywords, and additional core functions, but otherwise are compatible with previous code. The main limitation on utilizing new JavaScript features is whether the <em>interpreters</em> found in web browsers are able to support them.</p>
<p>This course primarily utilizes syntax and features for <code>ES5</code>, which was introduced in 2009 and today is supported by <a href="http://caniuse.com/#feat=es5">all modern browsers</a> (i.e., IE 10 or later). Later chapters will describe features of <code>ES6</code> (also called <code>ES2015</code>), which was introduced in 2015 and works on <a href="http://kangax.github.io/compat-table/es6/">many browsers</a> (e.g., Microsoft Edge but not IE 11). <code>ES7</code> was finalized in 2016 but is still not <a href="http://kangax.github.io/compat-table/esnext/">reliably supported</a>.</p>
<ul>
<li>Note that Microsoft no longer supports earlier versions of IE, but <a href="https://www.netmarketshare.com/browser-market-share.aspx?qprid=2&qpcustomd=0">a small percentage</a> of desktops still run Windows XP with IE 8. If you must support these browsers, you can often use a “transpiler” like <a href="https://babeljs.io/">Babel</a> to translate modern JavaScript source code into an earlier version.</li>
</ul>
</div>
</div>
<div id="running-javascript" class="section level2" number="10.2">
<h2><span class="header-section-number">10.2</span> Running JavaScript</h2>
<p>There are two primary ways of executing code written in JavaScript:</p>
<div id="in-the-browser" class="section level3 unnumbered">
<h3>In the Browser</h3>
<p>JavaScript scripts are most commonly 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 included in that HTML by using a <code><script></code> tag and specifying the <em>relative</em> path to the a file containing the code (usually a <strong><code>.js</code></strong> file) to execute. When the browser renders the HTML (reading top to bottom) and gets to that tag, it will download and execute the specified script file using the JavaScript interpreter:</p>
<pre class="language-html"><code><span class="token comment"><!-- execute the script.js file --></span>
<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>Notice that the <code><script></code> element is <em>not</em> empty! It is possible to include JavaScript code directly inside the tag, but this is considered bad practice (keep concerns separated!) and should only be used for quick tests.</li>
</ul>
<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 thus allows the JavaScript to immediately interact with the loaded HTML elements). We will (begin by) most commonly be putting <code><script></code> tags at the end of the <code><body></code>:</p>
<pre class="language-html"><code><span class="token doctype"><span class="token punctuation"><!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>head</span><span class="token punctuation">></span></span>
<span class="token comment"><!-- include here to run before the page appears --></span>
<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>js/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>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span>
... content ...
<span class="token comment"><!-- include here to run "after" html appears --></span>
<span class="token comment"><!-- we will usually do this --></span>
<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>js/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>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span><span class="token punctuation">></span></span></code></pre>
<ul>
<li>Most browsers also support adding a <code>defer</code> attribute to the <code><script></code> element that tells the browser to defer downloading and running the script until the page is fully loaded and rendered to the screen. With this attribute, you can put the <code><script></code> element in the <code><head></code> without any issues. However, this attribute is not supported in IE 9 or earlier; it is thus still best practice to put the <code><script></code> element at the end of the body.</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>
</div>
<div id="on-the-command-line-with-node.js" class="section level3 unnumbered">
<h3>On the Command-line with Node.js</h3>
<p>It is also possible to execute JavaScript code on the command line using <a href="https://nodejs.org/en/">Node.js</a>. Node is a <strong>command line runtime environment</strong>—that is, a JavaScript interpreter that can be run on the command line.</p>
<p>With Node installed on your machine, you can use the <strong><code>node</code></strong> terminal command to start an <em>interactive Node session</em>. This will allow you to type JavaScript code directly in the terminal, and your computer will interpret and execute each line of code:</p>
<div class="figure">
<img src="img/javascript/node-interactive-session.png" alt="" />
<p class="caption">An interactive Node session.</p>
</div>
<p>You can enter one line of code at a time at the prompt (<code>></code>). This is a nice way to experiment with the JavaScript language or to quickly run and test some code.</p>
<ul>
<li><p>You can exit this session by typing the <code>quit()</code> command, or hitting <code>ctrl-z</code> (followed by Enter on Windows).</p></li>
<li><p>Note that the JavaScript console in the Chrome Developer tools provides the same interactive functionality.</p></li>
</ul>
<p>It is also possible to run entire scripts (<code>.js</code> files) from the command line by using the <code>Node</code> command and specifying the script file you wish to execute:</p>
<pre class="language-bash"><code><span class="token function">node</span> my-script.js</code></pre>
<p>This allows you author entire programs in JavaScript (e.g., writing the code using VS Code), and then executing them from the command line. This process is more common when doing <em>server-side</em> web development (such as implementing a web server using Node), but we will still use it for practice and testing.</p>
</div>
</div>
<div id="writing-scripts" class="section level2" number="10.3">
<h2><span class="header-section-number">10.3</span> Writing Scripts</h2>
<p>Unlike Java or C, JavaScript has no <code>main()</code> method that “starts” the program.</p>
<p>Instead, a JavaScript script (a <code>.js</code> file) contains a sequence of <strong>statements</strong> (instructions) that the interpreter executes <em>in order, one at a time, top to bottom</em>—just as if you had typed them into an interactive session one after another. Each line can declare a variable or function, which will then be available to the next statement to use or call. Thus with a JavaScript program, you should think of the <em>sequence</em> of steps you want to be performed, and write lines of code in that order.</p>
<ul>
<li>In a way, you can think of the entire file as the “body” of a <code>main()</code> method… except that this body will contain further function declarations.</li>
</ul>
<p>For example:</p>
<pre class="language-js"><code><span class="token comment">/* script.js */</span>
<span class="token comment">/* This is the ENTIRE contents of the file! */</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 executed first</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"I'm doing JavaScript!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//this is executed second</span></code></pre>
<p>The above example code demonstrates the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Console/log"><strong><code>console.log()</code></strong></a> function, which is JavaScript’s equivalent to Java’s <code>System.out.println()</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 Java.</p>
<ul>
<li>The <code>console.log()</code> function is technically a <code>log()</code> method belonging being called on a <em>global</em> <code>console</code> object. Globals and objects will be discussed in more detail below.</li>
</ul>
<div style="float:right;margin-left:1em;">
<div class="figure">
<img src="img/javascript/hunting-semicolons.jpg" alt="" />
<p class="caption">Don’t forget the semicolons!</p>
</div>
</div>
<p>As in Java, each statement should end with a semicolon (<strong><code>;</code></strong>) marking the end of that statement. But unlike Java, JavaScript can be forgiving if you forget a semicolon. The JavaScript interpreter tries to be “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 in horrible ways—and so best practice as a developer is to <strong>always include the semicolons</strong>.</p>
<div id="strict-mode" class="section level3 unnumbered">
<h3>Strict Mode</h3>
<p><code>ES5</code> 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 <em>interpreter declaration</em> at the top:</p>
<pre class="language-js"><code><span class="token string">'use strict'</span><span class="token punctuation">;</span></code></pre>
<ul>
<li>This is not a String (or even JavaScript code!), but rather a <em>declaration</em> to the interpreter about how that interpreter should behave.</li>
</ul>
<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" class="section level2" number="10.4">
<h2><span class="header-section-number">10.4</span> Variables</h2>
<p>Variables in JavaScript are <strong>dynamically typed</strong>. This means that variables are <em>not</em> declared as a particular type (e.g., <code>int</code> or <code>String</code>), but instead take on the data type (<code>Number</code>, <code>String</code>, etc.) of the <em>value</em> currently assigned to that variable. As we don’t specify the data type, JavaScript variables are declared using the <code>let</code> keyword:</p>
<pre class="language-js"><code><span class="token keyword">let</span> message <span class="token operator">=</span> <span class="token string">'Hello World'</span><span class="token punctuation">;</span> <span class="token comment">//a String</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> message<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> `string`</span>
<span class="token keyword">let</span> shoeSize <span class="token operator">=</span> <span class="token number">7</span><span class="token punctuation">;</span> <span class="token comment">//a Number</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> shoeSize<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> 'number'</span></code></pre>
<ul>
<li><p>The <code>typeof</code> operator will return the data type of a variable. It is not widely used outside of debugging.</p></li>
<li><p>As in Java, JavaScript variables should be given descriptive names using <a href="https://en.wikipedia.org/wiki/Camel_case">camelCase</a>.</p></li>
</ul>
<p class="alert alert-info">
<strong>Pro Tip:</strong> Even though variables in JavaScript loosely typed, the data type of a value is still important! In order to help keep track of the type of each variable in JavaScript, include the type in the variable name. For example: <code>textString</code>, <code>wordsArray</code>, <code>totalNum</code>, <code>itemStr</code>, etc..
</p>
<p>Declared variables have a default value of <code>undefined</code>—a value representing that the variable has no value. This is somewhat similar to <code>null</code> in Java (though JavaScript <em>also</em> as an <code>null</code> value that is not commonly used):</p>
<pre class="language-js"><code><span class="token comment">//create a variable (not assigned)</span>
<span class="token keyword">let</span> hoursSlept<span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>hoursSlept<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> undefined</span></code></pre>
<p>Note that the <code>let</code> keyword is in fact new syntax introduced with <code>ES6</code>: older versions of JavaScript used the <code>var</code> keyword instead (and you will see <code>var</code> in most existing examples and tutorials). The difference is that variables declared with <code>let</code> are <strong>“block scoped”</strong>, meaning they are only available within the <em>block</em> (the <code>{}</code>) in which they are defined. This is the same way variables are scoped in Java. Variables declared with <code>var</code>, on the other hand, are “functionally scoped” so are available anywhere within the <em>function</em> in which they are defined. This means that you could declare a variable within an <code>if</code> block, and that variable would continue to be available outside that block! Thus <code>let</code> allows for cleaner, more efficient code, and with less bugs.</p>
<ul>
<li><code>let</code> is the one <code>ES6</code> feature supported by IE 11, meaning it can be used with <a href="http://caniuse.com/#search=let">most current browsers</a>. However, if you do need to support an older browser (e.g., IE 10, Safari 9.3, Android 4.4), you should transpile your code or stick to using <code>var</code></li>
</ul>
<p>Along with <code>let</code>, JavaScript variables can also be declared using the <code>const</code> keyword to indicate that they are <em>constant</em> (similar to what the <code>final</code> keyword does in Java). A <code>const</code> variable is block scoped, but can only be assigned once:</p>
<pre class="language-js"><code><span class="token keyword">const</span> <span class="token constant">ISCHOOL_URL</span> <span class="token operator">=</span> <span class="token string">'https://ischool.uw.edu'</span><span class="token punctuation">;</span> <span class="token comment">//declare constant</span>
<span class="token constant">ISCHOOL_URL</span> <span class="token operator">=</span> <span class="token string">'https://example.com'</span><span class="token punctuation">;</span> <span class="token comment">//TypeError: Assignment to constant variable.</span></code></pre>
<div id="basic-data-types" class="section level3 unnumbered">
<h3>Basic Data Types</h3>
<p>JavaScript supports many of the same basic data types as Java and other languages:</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 the same <em>mathematical</em> and operators as Java. 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 (similar to Java’s <code>Math</code> class).</p>
<pre class="language-js"><code><span class="token keyword">let</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">let</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 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—just be consistent! Strings can be concatenated as in Java:</p>
<pre class="language-js"><code><span class="token keyword">let</span> name <span class="token operator">=</span> <span class="token string">'Joel'</span><span class="token punctuation">;</span>
<span class="token keyword">let</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://www.w3schools.com/jsref/jsref_obj_string.asp">methods</a> for working with them. As with Java, JavaScript strings are <em>immutable</em>, so most string methods return a new, altered string.</p>
<pre class="language-js"><code><span class="token keyword">let</span> message <span class="token operator">=</span> <span class="token string">'Hello World'</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> shouted <span class="token operator">=</span> message<span class="token punctuation">.</span><span class="token function">toUpperCase</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>shouted<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> 'HELLO WORLD'</span></code></pre></li>
<li><p><strong>Booleans</strong> (<code>true</code> and <code>false</code>) in JavaScript work the same way as in Java, can be produced using the same <em>relational operators</em> (e.g., <code><</code>, <code>>=</code>, <code>!=</code>), and support the same <em>logical operators</em> (e.g., <code>&&</code>, <code>||</code>, and <code>!</code>):</p>
<pre class="language-js"><code><span class="token comment">//conjunction (and)</span>
boolOne <span class="token operator">&&</span> boolTwo
<span class="token comment">//disjunction (or)</span>
boolOne <span class="token operator">||</span> boolTwo
<span class="token comment">//negation (not)</span>
<span class="token operator">!</span>boolOne <span class="token comment">//not</span></code></pre>
<p>See <a href="javascript.html#type-coercion">Type Coercion</a> and <a href="javascript.html#control-structures">Control Structures</a> below for more on working with Booleans in JavaScript.</p></li>
</ul>
</div>
<div id="arrays" class="section level3 unnumbered">
<h3>Arrays</h3>
<p>JavaScript also supports <strong>arrays</strong>, which are <em>ordered, one-dimensional sequences of values</em>. As in Java, JavaScript Arrays 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 comment">//an array of names</span>
<span class="token keyword">let</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 comment">//an array of numbers (can contain "duplicate" values)</span>
<span class="token keyword">let</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">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</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 comment">//arrays can contain different types (including other arrays!)</span>
<span class="token keyword">let</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">3</span><span class="token punctuation">,</span> <span class="token number">4</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">//arrays can be empty (contain no elements)</span>
<span class="token keyword">let</span> empty <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">//access using bracket notation</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">// 3</span>
numbers<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">'340'</span><span class="token punctuation">;</span> <span class="token comment">//assign new value at index 0</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> numbers <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [340, 2, 2, 3, 5, 8]</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">let</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, similar to the <code>ArrayList</code> class in Java:</p>
<pre class="language-js"><code><span class="token comment">//Make a new array</span>
<span class="token keyword">let</span> array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'i'</span><span class="token punctuation">,</span><span class="token string">'n'</span><span class="token punctuation">,</span><span class="token string">'f'</span><span class="token punctuation">,</span><span class="token string">'o'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">//add item to end of the array</span>
array<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'340'</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><span class="token punctuation">;</span> <span class="token comment">//=> ['i','n','f','o','340']</span>
<span class="token comment">//combine elements into a string</span>
<span class="token keyword">let</span> str <span class="token operator">=</span> array<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'-'</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>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> "i-n-f-o-340"</span>
<span class="token comment">//get index of an element (first occurrence)</span>
<span class="token keyword">let</span> oIndex <span class="token operator">=</span> array<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'o'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> 3</span>
<span class="token comment">//remove 1 element starting at oIndex</span>
array<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>oIndex<span class="token punctuation">,</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>array<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> ['i','n','f','340']</span></code></pre>
</div>
<div id="objects" class="section level3 unnumbered">
<h3>Objects</h3>
<p>The most generic and useful data type in JavaScript is the Object data type. An <strong>Object</strong> is a lot like an array in that it is in that it is a (one-dimensional) sequence of values that are all stored in a single variable. However, rather than using <em>integers</em> as the index for each element, an Object uses <em>Strings</em>. Thus Objects are <em>unordered</em> sequences of <strong>key-value pairs</strong>, where the keys (called <strong>“properties”</strong>) are arbitrary Strings and the values are any data type—each property can be used to <em>look up</em> (reference) the value associated with it.</p>
<ul>
<li><p>This is a lot like a real-world dictionary or encyclopedia, in which the words (keys) are used to look up the definitions (values). A phone book works the same way (the names are the keys, the phone numbers are the values). A JavaScript Object provides a <em>mapping</em> from properties to values.</p></li>
<li><p>JavaScript Objects are similar data structures to Java <em>HashMaps</em>, Python <em>dictionaries</em>, R <em>lists</em>, or even an HTML <code><dl></code> descriptive list! More generally, these are known as <em>associative arrays</em> (they are arrays that “associate” a key and a value). As with other associative arrays, JavaScript Objects are implemented as <a href="https://en.wikipedia.org/wiki/Hash_table">hash tables</a>, making data access very fast.</p></li>
</ul>
<p>Objects are written as literals inside curly braces <strong><code>{}</code></strong>. Property-value pairs are written with a <em>colon</em> (<strong><code>:</code></strong>) between the property name and the value, and each element (pair) in the Object is separated by a <em>comma</em> (<strong><code>,</code></strong>). Note that the property names do <em>not</em> need to be written in quotes if they are a single word (the quotes are implied—properties are always 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">let</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">let</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">let</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">let</span> empty <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></code></pre>
<p><em>Important note:</em> Objects are an <strong>unordered</strong> collection of key-value pairs! Because you reference a value by its <em>property name</em> and not by its position (as you do in an array), the exact ordering of those elements doesn’t matter—the interpreter just goes immediately to the value associated with the property. This almost means that when you log out an Object, the order in which the properties are printed may not match the order in which you specified them in the literal.</p>
<ul>
<li><p>If you <code>console.log()</code> an object, you will usually see a nicely format version of that object. However, if you try to convert that object to a <em>string</em> (e.g., via concatenation: <code>let output = "my object: "+myObject</code>), you will instead be presented with the “string version” of that object: <code>[object Object]</code>. This is not an array, but the String version of an object (similar to the hash you get when you try and print a Java array directly). You can instead use the fact that <code>console.log</code> accepts <em>multiple parameters</em> to output an object with a leading string:</p>
<pre class="language-js"><code><span class="token keyword">let</span> myObject <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">a</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">b</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">}</span>
<span class="token comment">//convert object to string, won't log nicely</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"My object: "</span> <span class="token operator">+</span> myObject<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> My object: [object Object]</span>
<span class="token comment">//log the object directly</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"My object "</span><span class="token punctuation">,</span> myObject<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//=> My object {a: 1, b: 2}</span></code></pre></li>
</ul>
<p>Despite the name, JavaScript Objects have nothing to do with Object-Oriented Programing. However, they can be used to represent “things” in the same way as a Java object, with each property acting like an “attribute” (instance variable):</p>
<pre class="language-js"><code><span class="token comment">//an object representing a Person (spacing is for readability; white-space is ignored)</span>
<span class="token keyword">let</span> person <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token literal-property property">firstName</span><span class="token operator">:</span> <span class="token string">'Alice'</span><span class="token punctuation">,</span>
<span class="token literal-property property">lastName</span><span class="token operator">:</span> <span class="token string">'Smith'</span><span class="token punctuation">,</span>
<span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">40</span><span class="token punctuation">,</span>
<span class="token literal-property property">pets</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'rover'</span><span class="token punctuation">,</span> <span class="token string">'fluffy'</span><span class="token punctuation">,</span> <span class="token string">'mittens'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">//value is an array</span>
<span class="token literal-property property">favorites</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token comment">//value is another object</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 comment">//value is an array</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<div id="accessing-objects" class="section level4 unnumbered">
<h4>Accessing Objects</h4>
<p>Similar to arrays, Object values can be access via <strong>bracket notation</strong>, specifying the <em>property name</em> as the index. If an object does not explicitly have a property value, accessing that key produces <code>undefined</code> (the property’s value is <code>undefined</code>).</p>
<pre class="language-js"><code><span class="token keyword">let</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> <span class="token comment">//property name is a string</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>
<span class="token comment">//use a variable as the "key"</span>
<span class="token keyword">let</span> userInputtedTopic <span class="token operator">=</span> <span class="token string">'food'</span><span class="token punctuation">;</span> <span class="token comment">//pretend this value is supplied dynamically</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>favorites<span class="token punctuation">[</span>userInputtedTopic<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//'cake'</span></code></pre>
<p><em>Additionally</em>, Object values can also be accessed via <strong>dot notation</strong>, as if the properties were <em>public attributes</em> of a class instance. 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">let</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 only advantage to using <em>bracket notation</em> is that you can specify property names as variables or the results of an expression. But overall, 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 an <em>array</em> of an 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; a better approach is to iterate through the keys to identify all the values.</p>
<pre class="language-js"><code><span class="token keyword">let</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">let</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 id="arrays-of-objects" class="section level4 unnumbered">
<h4>Arrays of Objects</h4>
<p>As noted above, both arrays and objects can have values of any type—including other arrays or objects! The ability to nest objects inside of objects is incredibly powerful, and allows us to define arbitrarily complex information structurings (schemas). Indeed, most data in computer programs—as well as public information available on the web—is structured as a set of nested maps like this (though possibly with some level of abstraction).</p>
<p>One of the most common forms of nesting you’ll see is to have an <strong>array of objects</strong> where each object has <em>the same properties</em> (but different values). For example:</p>
<pre class="language-js"><code><span class="token comment">//an arbitrary list of people's names, heights, and weights</span>
<span class="token keyword">let</span> people <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Ada'</span><span class="token punctuation">,</span> <span class="token literal-property property">height</span><span class="token operator">:</span> <span class="token number">58</span><span class="token punctuation">,</span> <span class="token string-property property">'weight'</span><span class="token operator">:</span> <span class="token number">115</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Bob'</span><span class="token punctuation">,</span> <span class="token literal-property property">height</span><span class="token operator">:</span> <span class="token number">59</span><span class="token punctuation">,</span> <span class="token string-property property">'weight'</span><span class="token operator">:</span> <span class="token number">117</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Chris'</span><span class="token punctuation">,</span> <span class="token literal-property property">height</span><span class="token operator">:</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token string-property property">'weight'</span><span class="token operator">:</span> <span class="token number">120</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Diya'</span><span class="token punctuation">,</span> <span class="token literal-property property">height</span><span class="token operator">:</span> <span class="token number">61</span><span class="token punctuation">,</span> <span class="token string-property property">'weight'</span><span class="token operator">:</span> <span class="token number">123</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Emma'</span><span class="token punctuation">,</span> <span class="token literal-property property">height</span><span class="token operator">:</span> <span class="token number">62</span><span class="token punctuation">,</span> <span class="token string-property property">'weight'</span><span class="token operator">:</span> <span class="token number">126</span><span class="token punctuation">}</span>
<span class="token punctuation">]</span></code></pre>
<p>This structure can be seen as a list of <strong>records</strong> (the objects), each of which have a number of different <strong>features</strong> (the key-value pairs). This list of feature records is in fact a common way of understanding a <strong>data table</strong> like you would create as an Excel spreadsheet:</p>
<div class="figure">
<img src="img/javascript/excel-table.png" alt="" />
<p class="caption">A data table.</p>
</div>
<p>Each object (record) acts as a “row” in the table, and each property (feature) acts as a “column”. As long as all of the objects share the same keys, this array of objects <em>is</em> a table!</p>
</div>
</div>
<div id="type-coercion" class="section level3 unnumbered">
<h3>Type Coercion</h3>
<p>As mentioned above, variables in JavaScript are <em>dynamically typed</em>, and thus have a data type of the value <strong>currently</strong> assigned to that variable. JavaScript variables are able to “change type” by having a different type of data assignment to them:</p>
<pre class="language-js"><code><span class="token keyword">let</span> myVariable <span class="token operator">=</span> <span class="token string">'hello'</span><span class="token punctuation">;</span> <span class="token comment">//value is a String</span>
myVariable <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span> <span class="token comment">//value is now a Number</span></code></pre>
<p>Unlike some other dynamically typed variables, 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 process is similar to how Java will automatically cast data types into Strings (e.g., <code>"hello"+4</code>), JavaScript’s type coercion can produce a few quirks:</p>
<pre class="language-js"><code><span class="token keyword">let</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">let</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">let</span> num <span class="token operator">=</span> <span class="token number">10</span>
<span class="token keyword">let</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">let</span> num <span class="token operator">=</span> <span class="token number">10</span>
<span class="token keyword">let</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>
<p class="alert">
For more examples of the horror of JavaScript coercion, see <a href="https://www.destroyallsoftware.com/talks/wat">this video</a> (about 1:20 in).
</p>
</div>
</div>
<div id="control-structures" class="section level2" number="10.5">
<h2><span class="header-section-number">10.5</span> Control Structures</h2>
<p>JavaScript control structures have a similar syntax to those in Java or C. For example, a JavaScript <strong><code>if</code> statement</strong> is written as:</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>The <strong><code>condition</code></strong> can be any expression that evaluates to a Boolean value. 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>
<ul>
<li>While using <code>person.name !== undefined</code> is an equivalent expression, it is more idiomatic to utilize the coercion and have plain variable as the conditional expression.</li>
</ul>
<p>Additionally, JavaScript provides a <a href="https://en.wikipedia.org/wiki/%3F:">ternary conditional operator</a> that lets you write a simple <code>if</code> statement as a single expression:</p>
<pre class="language-js"><code><span class="token keyword">let</span> x<span class="token punctuation">;</span> <span class="token comment">//declare variable</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>condition<span class="token punctuation">)</span> <span class="token punctuation">{</span>
x <span class="token operator">=</span> <span class="token string">'foo'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
x <span class="token operator">=</span> <span class="token string">'bar'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//can be condensed into:</span>
<span class="token keyword">let</span> x <span class="token operator">=</span> condition <span class="token operator">?</span> <span class="token string">'foo'</span> <span class="token operator">:</span> <span class="token string">'bar'</span><span class="token punctuation">;</span></code></pre>
<ul>
<li>This expression is read as “<em>if</em> <code>condition</code> <em>then</em> resolve to <code>'foo'</code> <em>else</em> resolve to <code>bar</code>”. Each part (the condition, the “if true” result, and the “if false” result) can be any expression, though you should keep them simple to avoid confusion.</li>
</ul>
<p>JavaScript also supports <strong><code>while</code></strong> loop (for indefinite iteration) and <strong><code>for</code></strong> loops (for definite iteration) similar to Java. The only difference is that because JavaScript variables are dynamically typed, the <em>loop control variables</em> are not declared with a type:</p>
<pre class="language-js"><code><span class="token comment">//an example for loop. The `i` is not declared as an int</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</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>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. <code>ES6</code> also introduces a <code>for ... of</code> syntax for iterating over arrays, but this is not supported by all browsers and so is not recommended. Instead, the current best practice is to use the above <code>for</code> loop, or better yet the <code>forEach()</code> method described in the next chapter.</p>
<ul>
<li>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!</li>
</ul>
</div>
<div id="functions" class="section level2" number="10.6">
<h2><span class="header-section-number">10.6</span> Functions</h2>
<p>And of course, JavaScript includes <strong>functions</strong> (named sequences of statements used to <em>abstract</em> code). 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">let</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>
<span class="token comment">// Assign the result to `myName`</span>
<span class="token keyword">let</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>
<ul>
<li><p>Functions are defined by using the <strong><code>function</code></strong> keyword (placed before the name of the function) instead of Java’s <code>public static</code>, don’t declare a return type (since the language is dynamically typed), and don’t indicate types for the parameters. Otherwise, JavaScript functions have identical syntax to Java functions.</p></li>
<li><p>If a function lacks a <code>return</code> value, then that function returns the value <code>undefined</code>.</p></li>
</ul>