100% found this document useful (1 vote)
37 views47 pages

Eloquent JavaScript 3rd Edition Marijn Haverbeke - Download The Complete Ebook in PDF Format and Read Freely

The document provides links to download the full version of 'Eloquent JavaScript 3rd Edition' by Marijn Haverbeke, along with other recommended JavaScript-related ebooks and textbooks. It includes details about the book's content, licensing, and contributors. The document also lists additional resources for learning JavaScript and programming concepts.

Uploaded by

raghoddshp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
37 views47 pages

Eloquent JavaScript 3rd Edition Marijn Haverbeke - Download The Complete Ebook in PDF Format and Read Freely

The document provides links to download the full version of 'Eloquent JavaScript 3rd Edition' by Marijn Haverbeke, along with other recommended JavaScript-related ebooks and textbooks. It includes details about the book's content, licensing, and contributors. The document also lists additional resources for learning JavaScript and programming concepts.

Uploaded by

raghoddshp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Visit https://ebookultra.

com to download the full version and


explore more ebooks or textbooks

Eloquent JavaScript 3rd Edition Marijn Haverbeke

_____ Click the link below to download _____


https://ebookultra.com/download/eloquent-javascript-3rd-
edition-marijn-haverbeke/

Explore and download more ebooks or textbooks at ebookultra.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Eloquent JavaScript A Modern Introduction to Programming


1st Edition Marijn Haverbeke

https://ebookultra.com/download/eloquent-javascript-a-modern-
introduction-to-programming-1st-edition-marijn-haverbeke/

Eloquent JavaScript A Modern Introduction to Programming


2nd Edition Marijn Haverbeke

https://ebookultra.com/download/eloquent-javascript-a-modern-
introduction-to-programming-2nd-edition-marijn-haverbeke/

Beginning JavaScript 3rd ed Edition Paul Wilton

https://ebookultra.com/download/beginning-javascript-3rd-ed-edition-
paul-wilton/

JavaScript a beginner s guide 3rd ed Edition Pollock

https://ebookultra.com/download/javascript-a-beginner-s-guide-3rd-ed-
edition-pollock/
JavaScript jQuery The Missing Manual 3rd Edition David
Sawyer Mcfarland

https://ebookultra.com/download/javascript-jquery-the-missing-
manual-3rd-edition-david-sawyer-mcfarland/

Professional JavaScript for Web Developers 3rd Edition


Nicholas C. Zakas

https://ebookultra.com/download/professional-javascript-for-web-
developers-3rd-edition-nicholas-c-zakas/

Special Edition Using Javascript Mcfedries

https://ebookultra.com/download/special-edition-using-javascript-
mcfedries/

Pro JavaScript with MooTools Learning Advanced JavaScript


Programming 1st Edition Mark Joseph Obcena (Auth.)

https://ebookultra.com/download/pro-javascript-with-mootools-learning-
advanced-javascript-programming-1st-edition-mark-joseph-obcena-auth/

JavaScript Creativity Exploring the Modern Capabilities of


JavaScript and HTML5 1st ed. Edition Hudson

https://ebookultra.com/download/javascript-creativity-exploring-the-
modern-capabilities-of-javascript-and-html5-1st-ed-edition-hudson/
Eloquent JavaScript 3rd Edition Marijn Haverbeke
Digital Instant Download
Author(s): Marijn Haverbeke
ISBN(s): 9781593279509, 1593279507
Edition: 3
File Details: PDF, 2.16 MB
Year: 2018
Language: english
Eloquent JavaScript
3rd edition

Marijn Haverbeke
Copyright © 2018 by Marijn Haverbeke

This work is licensed under a Creative Commons attribution-noncommercial


license (http://creativecommons.org/licenses/by-nc/3.0/). All code in the
book may also be considered licensed under an MIT license (https://eloquentjavascript.
net/code/LICENSE).
The illustrations are contributed by various artists: Cover and chapter illus-
trations by Madalina Tantareanu. Pixel art in Chapters 7 and 16 by Antonio
Perdomo Pastor. Regular expression diagrams in Chapter 9 generated with
regexper.com by Jeff Avallone. Village photograph in Chapter 11 by Fabrice
Creuzot. Game concept for Chapter 16 by Thomas Palef.
The third edition of Eloquent JavaScript was made possible by 325 financial
backers.

You can buy a print version of this book, with an extra bonus chapter included,
printed by No Starch Press at http://a-fwd.com/com=marijhaver-20&asin-
com=1593279507.

i
Contents
Introduction 1
On programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Why language matters . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
What is JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Code, and what to do with it . . . . . . . . . . . . . . . . . . . . . . . 7
Overview of this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Typographic conventions . . . . . . . . . . . . . . . . . . . . . . . . . . 8

1 Values, Types, and Operators 10


Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Unary operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Empty values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Automatic type conversion . . . . . . . . . . . . . . . . . . . . . . . . . 18
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

2 Program Structure 22
Expressions and statements . . . . . . . . . . . . . . . . . . . . . . . . 22
Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Binding names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The console.log function . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Control flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
while and do loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Indenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Breaking Out of a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 33

ii
Updating bindings succinctly . . . . . . . . . . . . . . . . . . . . . . . 34
Dispatching on a value with switch . . . . . . . . . . . . . . . . . . . . 34
Capitalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

3 Functions 39
Defining a function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Bindings and scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Functions as values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Declaration notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Arrow functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
The call stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Optional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Closure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Growing functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Functions and side effects . . . . . . . . . . . . . . . . . . . . . . . . . 54
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

4 Data Structures: Objects and Arrays 57


The weresquirrel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Data sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Mutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The lycanthrope’s log . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Computing correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Array loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
The final analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Further arrayology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Strings and their properties . . . . . . . . . . . . . . . . . . . . . . . . 72
Rest parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
The Math object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

iii
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

5 Higher-Order Functions 82
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Abstracting repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Higher-order functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Script data set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Filtering arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Transforming with map . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Summarizing with reduce . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Composability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Strings and character codes . . . . . . . . . . . . . . . . . . . . . . . . 91
Recognizing text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

6 The Secret Life of Objects 97


Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Class notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Overriding derived properties . . . . . . . . . . . . . . . . . . . . . . . 103
Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
The iterator interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Getters, setters, and statics . . . . . . . . . . . . . . . . . . . . . . . . 110
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
The instanceof operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

7 Project: A Robot 117


Meadowfield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
The task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Persistent data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
The mail truck’s route . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Pathfinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

iv
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

8 Bugs and Errors 128


Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Strict mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Error propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Cleaning up after exceptions . . . . . . . . . . . . . . . . . . . . . . . . 136
Selective catching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

9 Regular Expressions 143


Creating a regular expression . . . . . . . . . . . . . . . . . . . . . . . 143
Testing for matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Sets of characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Repeating parts of a pattern . . . . . . . . . . . . . . . . . . . . . . . . 146
Grouping subexpressions . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Matches and groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
The Date class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Word and string boundaries . . . . . . . . . . . . . . . . . . . . . . . . 150
Choice patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
The mechanics of matching . . . . . . . . . . . . . . . . . . . . . . . . 151
Backtracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
The replace method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Greed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Dynamically creating RegExp objects . . . . . . . . . . . . . . . . . . 157
The search method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
The lastIndex property . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Parsing an INI file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
International characters . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

10 Modules 167
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

v
Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Improvised modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Evaluating data as code . . . . . . . . . . . . . . . . . . . . . . . . . . 170
CommonJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
ECMAScript modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Building and bundling . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Module design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

11 Asynchronous Programming 180


Asynchronicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Crow tech . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Networks are hard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Collections of promises . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Network flooding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Message routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Async functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
The event loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Asynchronous bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

12 Project: A Programming Language 202


Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
The evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Special forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
The environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Cheating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

13 JavaScript and the Browser 216


Networks and the Internet . . . . . . . . . . . . . . . . . . . . . . . . . 216
The Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

vi
HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
HTML and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
In the sandbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Compatibility and the browser wars . . . . . . . . . . . . . . . . . . . 222

14 The Document Object Model 224


Document structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Moving through the tree . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Finding elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Changing the document . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Creating nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Cascading styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Query selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Positioning and animating . . . . . . . . . . . . . . . . . . . . . . . . . 238
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

15 Handling Events 243


Event handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Events and DOM nodes . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Event objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Default actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Key events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Pointer events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Scroll events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Focus events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Load event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Events and the event loop . . . . . . . . . . . . . . . . . . . . . . . . . 255
Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Debouncing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

vii
16 Project: A Platform Game 261
The game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
The technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Reading a level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Encapsulation as a burden . . . . . . . . . . . . . . . . . . . . . . . . . 268
Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Motion and collision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Actor updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Tracking keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Running the game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

17 Drawing on Canvas 284


SVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
The canvas element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Lines and surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Drawing a pie chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Storing and clearing transformations . . . . . . . . . . . . . . . . . . . 297
Back to the game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Choosing a graphics interface . . . . . . . . . . . . . . . . . . . . . . . 304
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

18 HTTP and Forms 308


The protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Browsers and HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Fetch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
HTTP sandboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Appreciating HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Security and HTTPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Form fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Disabled fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

viii
The form as a whole . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Text fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Checkboxes and radio buttons . . . . . . . . . . . . . . . . . . . . . . . 321
Select fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
File fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Storing data client-side . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328

19 Project: A Pixel Art Editor 330


Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
The state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
DOM building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
The canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
The application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Drawing tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Saving and loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Undo history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Let’s draw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Why is this so hard? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

20 Node.js 350
Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
The node command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Installing with NPM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
The file system module . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
The HTTP module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
A file server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

21 Project: Skill-Sharing Website 369


Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Long polling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
HTTP interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
The server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
The client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380

ix
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

Exercise Hints 388


Program Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Data Structures: Objects and Arrays . . . . . . . . . . . . . . . . . . . 390
Higher-Order Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
The Secret Life of Objects . . . . . . . . . . . . . . . . . . . . . . . . . 393
Project: A Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Bugs and Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Asynchronous Programming . . . . . . . . . . . . . . . . . . . . . . . . 398
Project: A Programming Language . . . . . . . . . . . . . . . . . . . . 399
The Document Object Model . . . . . . . . . . . . . . . . . . . . . . . 400
Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Project: A Platform Game . . . . . . . . . . . . . . . . . . . . . . . . . 402
Drawing on Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
HTTP and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Project: A Pixel Art Editor . . . . . . . . . . . . . . . . . . . . . . . . 406
Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Project: Skill-Sharing Website . . . . . . . . . . . . . . . . . . . . . . . 409

x
“We think we are creating the system for our own purposes. We
believe we are making it in our own image... But the computer is
not really like us. It is a projection of a very slim part of ourselves:
that portion devoted to logic, order, rule, and clarity.”
—Ellen Ullman, Close to the Machine: Technophilia and its
Discontents

Introduction
This is a book about instructing computers. Computers are about as common
as screwdrivers today, but they are quite a bit more complex, and making them
do what you want them to do isn’t always easy.
If the task you have for your computer is a common, well-understood one,
such as showing you your email or acting like a calculator, you can open the
appropriate application and get to work. But for unique or open-ended tasks,
there probably is no application.
That is where programming may come in. Programming is the act of con-
structing a program—a set of precise instructions telling a computer what to do.
Because computers are dumb, pedantic beasts, programming is fundamentally
tedious and frustrating.
Fortunately, if you can get over that fact, and maybe even enjoy the rigor
of thinking in terms that dumb machines can deal with, programming can be
rewarding. It allows you to do things in seconds that would take forever by
hand. It is a way to make your computer tool do things that it couldn’t do
before. And it provides a wonderful exercise in abstract thinking.
Most programming is done with programming languages. A programming
language is an artificially constructed language used to instruct computers. It
is interesting that the most effective way we’ve found to communicate with a
computer borrows so heavily from the way we communicate with each other.
Like human languages, computer languages allow words and phrases to be
combined in new ways, making it possible to express ever new concepts.
At one point language-based interfaces, such as the BASIC and DOS prompts
of the 1980s and 1990s, were the main method of interacting with computers.
They have largely been replaced with visual interfaces, which are easier to learn
but offer less freedom. Computer languages are still there, if you know where
to look. One such language, JavaScript, is built into every modern web browser
and is thus available on almost every device.
This book will try to make you familiar enough with this language to do
useful and amusing things with it.

1
On programming
Besides explaining JavaScript, I will introduce the basic principles of program-
ming. Programming, it turns out, is hard. The fundamental rules are simple
and clear, but programs built on top of these rules tend to become complex
enough to introduce their own rules and complexity. You’re building your own
maze, in a way, and you might just get lost in it.
There will be times when reading this book feels terribly frustrating. If you
are new to programming, there will be a lot of new material to digest. Much of
this material will then be combined in ways that require you to make additional
connections.
It is up to you to make the necessary effort. When you are struggling to follow
the book, do not jump to any conclusions about your own capabilities. You are
fine—you just need to keep at it. Take a break, reread some material, and make
sure you read and understand the example programs and exercises. Learning is
hard work, but everything you learn is yours and will make subsequent learning
easier.

When action grows unprofitable, gather information; when infor-


mation grows unprofitable, sleep.
—Ursula K. Le Guin, The Left Hand of Darkness

A program is many things. It is a piece of text typed by a programmer,


it is the directing force that makes the computer do what it does, it is data
in the computer’s memory, yet it controls the actions performed on this same
memory. Analogies that try to compare programs to objects we are familiar
with tend to fall short. A superficially fitting one is that of a machine—lots of
separate parts tend to be involved, and to make the whole thing tick, we have
to consider the ways in which these parts interconnect and contribute to the
operation of the whole.
A computer is a physical machine that acts as a host for these immaterial
machines. Computers themselves can do only stupidly straightforward things.
The reason they are so useful is that they do these things at an incredibly
high speed. A program can ingeniously combine an enormous number of these
simple actions to do very complicated things.
A program is a building of thought. It is costless to build, it is weightless,
and it grows easily under our typing hands.
But without care, a program’s size and complexity will grow out of control,
confusing even the person who created it. Keeping programs under control is
the main problem of programming. When a program works, it is beautiful. The

2
art of programming is the skill of controlling complexity. The great program
is subdued—made simple in its complexity.
Some programmers believe that this complexity is best managed by using
only a small set of well-understood techniques in their programs. They have
composed strict rules (“best practices”) prescribing the form programs should
have and carefully stay within their safe little zone.
This is not only boring, it is ineffective. New problems often require new
solutions. The field of programming is young and still developing rapidly, and
it is varied enough to have room for wildly different approaches. There are
many terrible mistakes to make in program design, and you should go ahead
and make them so that you understand them. A sense of what a good program
looks like is developed in practice, not learned from a list of rules.

Why language matters


In the beginning, at the birth of computing, there were no programming lan-
guages. Programs looked something like this:
00110001 00000000 00000000
00110001 00000001 00000001
00110011 00000001 00000010
01010001 00001011 00000010
00100010 00000010 00001000
01000011 00000001 00000000
01000001 00000001 00000001
00010000 00000010 00000000
01100010 00000000 00000000

That is a program to add the numbers from 1 to 10 together and print out
the result: 1 + 2 + ... + 10 = 55. It could run on a simple, hypothetical
machine. To program early computers, it was necessary to set large arrays of
switches in the right position or punch holes in strips of cardboard and feed
them to the computer. You can probably imagine how tedious and error-prone
this procedure was. Even writing simple programs required much cleverness
and discipline. Complex ones were nearly inconceivable.
Of course, manually entering these arcane patterns of bits (the ones and
zeros) did give the programmer a profound sense of being a mighty wizard.
And that has to be worth something in terms of job satisfaction.
Each line of the previous program contains a single instruction. It could be
written in English like this:

3
1. Store the number 0 in memory location 0.
2. Store the number 1 in memory location 1.
3. Store the value of memory location 1 in memory location 2.
4. Subtract the number 11 from the value in memory location 2.
5. If the value in memory location 2 is the number 0, continue with instruc-
tion 9.
6. Add the value of memory location 1 to memory location 0.
7. Add the number 1 to the value of memory location 1.
8. Continue with instruction 3.
9. Output the value of memory location 0.

Although that is already more readable than the soup of bits, it is still rather
obscure. Using names instead of numbers for the instructions and memory
locations helps.
Set “total” to 0.
Set “count” to 1.
[loop]
Set “compare” to “count”.
Subtract 11 from “compare”.
If “compare” is zero, continue at [end].
Add “count” to “total”.
Add 1 to “count”.
Continue at [loop].
[end]
Output “total”.

Can you see how the program works at this point? The first two lines give
two memory locations their starting values: total will be used to build up the
result of the computation, and count will keep track of the number that we are
currently looking at. The lines using compare are probably the weirdest ones.
The program wants to see whether count is equal to 11 to decide whether it
can stop running. Because our hypothetical machine is rather primitive, it can
only test whether a number is zero and make a decision based on that. So it
uses the memory location labeled compare to compute the value of count - 11
and makes a decision based on that value. The next two lines add the value

4
of count to the result and increment count by 1 every time the program has
decided that count is not 11 yet.
Here is the same program in JavaScript:
let total = 0, count = 1;
while (count <= 10) {
total += count;
count += 1;
}
console.log(total);
// → 55

This version gives us a few more improvements. Most important, there is


no need to specify the way we want the program to jump back and forth
anymore. The while construct takes care of that. It continues executing the
block (wrapped in braces) below it as long as the condition it was given holds.
That condition is count <= 10, which means “count is less than or equal to 10”.
We no longer have to create a temporary value and compare that to zero, which
was just an uninteresting detail. Part of the power of programming languages
is that they can take care of uninteresting details for us.
At the end of the program, after the while construct has finished, the console
.log operation is used to write out the result.
Finally, here is what the program could look like if we happened to have
the convenient operations range and sum available, which respectively create a
collection of numbers within a range and compute the sum of a collection of
numbers:
console.log(sum(range(1, 10)));
// → 55

The moral of this story is that the same program can be expressed in both
long and short, unreadable and readable ways. The first version of the program
was extremely obscure, whereas this last one is almost English: log the sum of
the range of numbers from 1 to 10. (We will see in later chapters how to define
operations like sum and range.)
A good programming language helps the programmer by allowing them to
talk about the actions that the computer has to perform on a higher level.
It helps omit details, provides convenient building blocks (such as while and
console.log), allows you to define your own building blocks (such as sum and
range), and makes those blocks easy to compose.

5
What is JavaScript?
JavaScript was introduced in 1995 as a way to add programs to web pages in the
Netscape Navigator browser. The language has since been adopted by all other
major graphical web browsers. It has made modern web applications possible—
applications with which you can interact directly without doing a page reload
for every action. JavaScript is also used in more traditional websites to provide
various forms of interactivity and cleverness.
It is important to note that JavaScript has almost nothing to do with the
programming language named Java. The similar name was inspired by mar-
keting considerations rather than good judgment. When JavaScript was being
introduced, the Java language was being heavily marketed and was gaining
popularity. Someone thought it was a good idea to try to ride along on this
success. Now we are stuck with the name.
After its adoption outside of Netscape, a standard document was written
to describe the way the JavaScript language should work so that the various
pieces of software that claimed to support JavaScript were actually talking
about the same language. This is called the ECMAScript standard, after the
Ecma International organization that did the standardization. In practice, the
terms ECMAScript and JavaScript can be used interchangeably—they are two
names for the same language.
There are those who will say terrible things about JavaScript. Many of these
things are true. When I was required to write something in JavaScript for the
first time, I quickly came to despise it. It would accept almost anything I typed
but interpret it in a way that was completely different from what I meant. This
had a lot to do with the fact that I did not have a clue what I was doing, of
course, but there is a real issue here: JavaScript is ridiculously liberal in what
it allows. The idea behind this design was that it would make programming in
JavaScript easier for beginners. In actuality, it mostly makes finding problems
in your programs harder because the system will not point them out to you.
This flexibility also has its advantages, though. It leaves space for a lot of
techniques that are impossible in more rigid languages, and as you will see
(for example in Chapter 10), it can be used to overcome some of JavaScript’s
shortcomings. After learning the language properly and working with it for a
while, I have learned to actually like JavaScript.
There have been several versions of JavaScript. ECMAScript version 3 was
the widely supported version in the time of JavaScript’s ascent to dominance,
roughly between 2000 and 2010. During this time, work was underway on
an ambitious version 4, which planned a number of radical improvements and
extensions to the language. Changing a living, widely used language in such a

6
radical way turned out to be politically difficult, and work on the version 4 was
abandoned in 2008, leading to a much less ambitious version 5, which made
only some uncontroversial improvements, coming out in 2009. Then in 2015
version 6 came out, a major update that included some of the ideas planned
for version 4. Since then we’ve had new, small updates every year.
The fact that the language is evolving means that browsers have to constantly
keep up, and if you’re using an older browser, it may not support every feature.
The language designers are careful to not make any changes that could break
existing programs, so new browsers can still run old programs. In this book,
I’m using the 2017 version of JavaScript.
Web browsers are not the only platforms on which JavaScript is used. Some
databases, such as MongoDB and CouchDB, use JavaScript as their scripting
and query language. Several platforms for desktop and server programming,
most notably the Node.js project (the subject of Chapter 20), provide an envi-
ronment for programming JavaScript outside of the browser.

Code, and what to do with it


Code is the text that makes up programs. Most chapters in this book contain
quite a lot of code. I believe reading code and writing code are indispensable
parts of learning to program. Try to not just glance over the examples—read
them attentively and understand them. This may be slow and confusing at
first, but I promise that you’ll quickly get the hang of it. The same goes for
the exercises. Don’t assume you understand them until you’ve actually written
a working solution.
I recommend you try your solutions to exercises in an actual JavaScript
interpreter. That way, you’ll get immediate feedback on whether what you are
doing is working, and, I hope, you’ll be tempted to experiment and go beyond
the exercises.
The easiest way to run the example code in the book, and to experiment with
it, is to look it up in the online version of the book at https://eloquentjavascript.net.
There, you can click any code example to edit and run it and to see the output
it produces. To work on the exercises, go to https://eloquentjavascript.net/
code, which provides starting code for each coding exercise and allows you to
look at the solutions.
If you want to run the programs defined in this book outside of the book’s
website, some care will be required. Many examples stand on their own and
should work in any JavaScript environment. But code in later chapters is
often written for a specific environment (the browser or Node.js) and can run

7
only there. In addition, many chapters define bigger programs, and the pieces
of code that appear in them depend on each other or on external files. The
sandbox on the website provides links to Zip files containing all the scripts and
data files necessary to run the code for a given chapter.

Overview of this book


This book contains roughly three parts. The first 12 chapters discuss the
JavaScript language. The next seven chapters are about web browsers and the
way JavaScript is used to program them. Finally, two chapters are devoted to
Node.js, another environment to program JavaScript in.
Throughout the book, there are five project chapters, which describe larger
example programs to give you a taste of actual programming. In order of
appearance, we will work through building a delivery robot, a programming
language, a platform game, a pixel paint program, and a dynamic website.
The language part of the book starts with four chapters that introduce the
basic structure of the JavaScript language. They introduce control structures
(such as the while word you saw in this introduction), functions (writing your
own building blocks), and data structures. After these, you will be able to write
basic programs. Next, Chapters 5 and 6 introduce techniques to use functions
and objects to write more abstract code and keep complexity under control.
After a first project chapter, the language part of the book continues with
chapters on error handling and bug fixing, regular expressions (an important
tool for working with text), modularity (another defense against complexity),
and asynchronous programming (dealing with events that take time). The
second project chapter concludes the first part of the book.
The second part, Chapters 13 to 19, describes the tools that browser JavaScript
has access to. You’ll learn to display things on the screen (Chapters 14 and
17), respond to user input (Chapter 15), and communicate over the network
(Chapter 18). There are again two project chapters in this part.
After that, Chapter 20 describes Node.js, and Chapter 21 builds a small
website using that tool.

Typographic conventions
In this book, text written in a monospaced font will represent elements of
programs—sometimes they are self-sufficient fragments, and sometimes they
just refer to part of a nearby program. Programs (of which you have already
seen a few) are written as follows:

8
Other documents randomly have
different content
and the remainder is Úbygð (hod. Obygð) or desert—a chaos of sand-tracts and peat-swamps, lava-runs,
and the huge masses of eternal congelation called Jökulls.[154]
The population was laid down by Barrow (1834) at 0·2 per whole area, and by Paijkull (1865) at
1·6: being now assumed at 70,000, it would be 1·75. Paijkull makes 6·2 head the average of habitable
ground, and for the reclaimed tracts he gives 17·5. The latter figure exceeds the mean of Africa, which is
16 to the square mile (viz, 192,000,000 head to 11,556,000 square statute miles), and it is three times
greater than in the whole Western Hemisphere.

§ 2. Divisions.

In early Norwegian days (A.D. 965) Iceland was distributed, like Ireland, into four quadrants, tetrads,
or fourths (Fjórðungar), named after the points of the compass. These were—

Austfirðinga-fjórðungr, Eastern Quarter.


Vestfirðinga- ” Western ”
Norðlendinga- ” Northern ”
Sunnlendinga- ” Southern ”

Before A.D. 1770. one Amtmaðr governed the whole of Iceland; in that year it was divided into two
Amts (rules), the north-eastern and the south-western. Thus the northern and the eastern quadrants,
whose population was scanty, were placed for administrative purposes under a single Amt, the
headquarters being at Fríðriksgáfa, of old Möðruvellir, near Akureyri, on the western shore of the
Eyjafjörð. In 1787 the south-west Amt was divided into two, the southern and the western. In 1872 it
was proposed to unite the western with the southern tetrarchy, and to transfer the amtship of
Stykkishólm to Reykjavik, the capital. Thus there will again be only two Amts under the governor, and
this simplification may act well.
The official title of the highest official was Stiptamtmaðr; in Danish, Stiftamtmand.[155] It has lately
been changed, without, however, any other advantage of rank or pay, from High Bailiff to Governor-
General (Landshöfðingi). Formerly the military and naval services had a preference, and titled names
were not rare: at present the post is given to civilians.[156] The salary of this high official was $500 in
1772; it afterwards became $2000, and now it is $4000.
The four quarters were divided into Sýslur[157] (Dan. Syssel), which are ever changing. For
instance, the Gullbróngu and Kjósar have lately been united, politically as well as ecclesiastically; the
same has happened to Mýra Sýsla and Hnappadals, whilst the vacancies have been filled up by the
Vestmannaeyjar. Under the twenty-one Sýslur, cantons or counties, prefectures or sheriffdoms, are the
169 Hrepps or poor-law districts,[158] which are not like our ecclesiastic divisions. We have preserved in
England the word,e.g., Rape of Brambor.
The following is a list of Sýslur and Hreppar, taken from the official documents which show the
movement of Iceland in 1868.[159]
The Suður-umdæmið, or southern jurisdiction, contains 7 Sýslur and 48 Hreppar, viz.:

1. Austur-Skaptafells Sysla, with 7Hreppar.


2. Vestur-Skaptafells ” with 7Hreppar.
3. Vestmannaeyjar ” ” 1 ”
4. Rángárvalla ” ” 8 ”
5. Árnes (not Arness) ” ” 13 ”
6. Gullbríngu and Kjósar ” ” 9 ”
7. Reykjavik ” ” 1 ”
8. Borgarfjarðar ” ” 9 ”
The Vestur-umdæmið contains 6 Sýslur and 55 Hreppar, viz.:

1. Mýra and Hnappadals Sýslur, with 10 Hreppar.


2. Snæfellsnes(not Snoefells) Sysla, ” 7 ”
3. Dala ” ” 8 ”
4. Barðastrandar ” ” 10 ”
5. Ísafjarðar ” ” 14 ”
6. Stranda ” ” 6 ”

The Norður og Austur Umdæmið contains 7 Sýslur and 66 Hreppar, viz.:

1. Húnavatns Sýsla, with 12Hreppar.


2. Skagafjarðar ” ” 12 ”
3. Eyjafjarðar(Grimsey, etc.), ” ” 10 ”
4. Suður-Thingeyjar ” ” 12 ”
5. Norður-Thingeyjar ” ” 12 ”
6. Norður-Múla[160] ” ” 10 ”
7. Suður-Múla ” ” 10 ”

When the author visited Iceland (1872), the Bæarfógeti, or mayor of Reykjavik, was Amtmaðr for
the southern quarter. Hr Christián Christiánsson ruled the north and east at Fríðriksgáfa, and Hr Bergur
Thorberg, knight of the Dannebrog, had his headquarters at Stykkishólm on the western fourth. Now
(1874), Hr Bergur Thorberg governs the southern and western quadrants, and Hr Christían
Christiánsson, with the title of Justitsráð, the northern and the eastern. These officers are addressed as
Hávelborni, and they receive the reports of the several Sýslumenn.
The Sýlumenn, or sheriffs, are the civil staff, the tax-gatherers and stewards as it were of the king;
and appointed by the Crown. In order to obtain this office they must be graduates of the University of
Copenhagen; they wear uniforms, a gold band round the cap, frock coats, waistcoats, and vests of blue
broadcloth, with the royal button, and they may become ministers of state. They preside at the
Hèraðthings[161] or annual county courts; they watch over the peace of their shrievalties; they officiate
as public notaries; and they maintain the rights of inheritance. The Sýslumaðr in his judicial capacity,
and chiefly when land-questions are to be determined, is occasionally assisted by four Meðdómsmenn
(concessores judicii), who give suffrage and register proceedings; decisions are pronounced according to
the vote of the majority.[162] He superintends elections. Formerly he could compel the lieges to repair
the highways, and the law still obliges each landed proprietor to keep the rough fences upon his estate in
good condition. A small sum called Vegabótargjald is also taken by the Sýslumaðr to pay for the
necessary expenses of roads; unfortunately the corvée or robot of peasants has been abolished, and the
means of transit are much neglected. A law compelling all sturdy vagrants and able-bodied paupers to
work upon the highways is as much wanted in Iceland, as useful and productive employment for the
hordes of soldiers who now compose the standing armies of Europe.
Under the Sýslumenn and appointed by the Amtmenn are the Hreppstjórar or Hreppstjórnarmenn,
bailiffs and poor-inspectors with parochial jurisdiction. It is hardly to be doubted that the division into
Rapes existed in heathen days, and Dr Konrad Maurer believes that they had organised poor laws and
rules for vagrancy which the Christian bishops afterwards amended and expanded. In these days the
Rape-stewards assist their civil and ecclesiastical superiors to manage the business of the Rape, to
preserve public order, and to estimate cessable property according to the ancient custom of the island.
They fix the poor-rate for each land-holder, and they especially attend to the condition and maintenance
of paupers (Úmagar), who are no longer subject to the pains and penalties of that ancient code the
Grágás (grey or wild goose).[163] Where the parish exceeds 400 souls, these minor officials usually
number two to five. They are substantial yeomen who wear no distinctive dress. They and their children
are exempt from taxation, and this is their only salary. The functions of the Amtmenn, Sýslumenn, and
of the Hreppstjórar especially, will be greatly modified when the law of May 4th, 1872, comes into
operation during the present year. A standing Hreppsnefnd, or a committee of three, five, or seven, is to
be elected in each Hreppr. This body is to have charge of the poor, the sanitary conditions, and the
general business of the Hreppr, including the repair of roads. It is also to levy the poor-rates and other
cesses of the Hreppr. The Hreppstjórar will be retained, but their functions are not defined. A Sýslunefnd
is also to be elected in each Sýsla, consisting of six to ten members; and the Sýslumaðr is ex officio a
convener or foreman of this committee. It is to have charge of the roads, to manage the general business
of the Sýsla, and to exercise supervision over the Hreppsnefndir. Thirdly, Amtsrað, Amt-Councils,
consisting of the Amtmaðr and two elected members, will audit and control all the accounts of the Amt;
will act as trustees of all public institutions and public legacies, and will have supervision over the
Hreppsnefndir and Sýslunefndir.

§ 3. Judicial Procedure.

It is well known that trial by jury, the bulwark of Englishmen’s rights, though fathered by English
legal antiquaries upon King Alfred, is a purely Scandinavian institution. According to the Landnámabók
(II., ix., note, p. 83), the Kviðr plays a considerable part in the republican history; and the form of trial
like our juries de vicineto appears in the thirteenth century. As Mr Vigfússon remarks (Cleasby, sub voce
Kviðr): “From the analogy of the Icelandic customs, it can be inferred with certainty that, along with the
invasion of Danes and Norsemen, the judgment by verdict was also transplanted to English ground, for
the settlers of England were kith and kin to those of Iceland, carrying with them the same laws and
customs; lastly, after the Conquest, it became the law of the land. This old Scandinavian institution
gradually died out in the mother countries[164] and ended in Iceland, A.D. 1271-1281, with the fall of the
Commonwealth and the introduction of a Norse code of laws, whereas it was naturalised in England,
which came to be the classical land of trial by jury.”
Modern Iceland utterly ignores it, but, as in the United States, all freemen are familiar with judicial
procedures, and public opinion, not to speak of the press, is a sufficient safeguard for a small
community.
In criminal cases the Crown prosecutes, and the king must ratify capital sentences. Like the Cives of
Rome, and very unlike the subjects of civilised Europe, Icelanders are not confined before trial, there
being no houses of detention; but a criminal is kept either by the sheriff or the hreppstjóri, who is
responsible for his being brought to judgment at an order from the court. By way of checking the
litigiousness of the lieges, a regular system of arbitration is in force. The parish priest ex officio and one
of his parishioners are the Forlíkunarmenn (reconciliators), and act as umpires; and a previous
investigation of causes often quashes them.
It is only in administrative cases, e.g., about paupers, etc., that there is an appeal from the decisions
of the sheriff to the Amtmaðr. From the Sýslumaðr’s court civil causes go for cassation directly to the
Supreme Court (Konunglegi-Landsyfirrettur) of Reykjavik, which was instituted in A.D. 1800, when the
Althing, which then had judicial as well as legislative and administrative functions, was abolished. The
Royal Court consists of a Chief Justice (Justiciarius) and two assessors; the governor presides, but takes
no part in the judicial proceedings. All three votes are equal, and the majority decides, thus making the
judge and assessors jury as well as judges. The actual dignitaries are Hr Thórður Jonasson, Hr Jón
Pétursen, and Hr Magnús Stephensen; the salaries are, $2816, $2016, and $1416. There are also two
procurators (the English barrister and the Scotch advocate), Hr Páll Melsted and Hr Jón Guðmundsson,
who edits the leading newspaper. Hr P. Guðjónsson, the church organist, is not a procurator although he
occasionally conducts cases before the superior court.
At this Royal High Court of Judgment the evidence and pleadings of both parties are heard, and the
Justiciarius, after taking the opinions of his assessors, pronounces his decision. For cassation, causes
must then go to the Chancellerie, or Supreme Court of Judicature at Copenhagen.
SECTION V.

ANTHROPOLOGY.

Statistics—General Considerations—Personal Appearance—Character—The Family—


Diseases.

§ 1. Statistics.

The constitution of society and the physical features of Iceland are peculiarly favourable to
numbering the people. The island has no object either to diminish her total in order to avoid recruiting,
and has scant interest in exaggerating it with a view to urban concessions and civic privileges. Between
A.D. 1840-60 the census was quinquennial; since that time every decade has been deemed sufficient.
The following numbers are taken from various sources, and especially from the latest official
figures in the Skýrslur of October 1, 1870:

O. Olavius Ponteppidan Thaarup, etc.


S. Qr. W. Qr. N. & E. Qrs. Total.
In A.D. 1703,18,728 15,774 15,942 50,444
” 1769,17,150 13,596 15,455 46,201

In A.D. 1770 Uno Von Troil (p. 25) estimated the population at 60,000 souls, or about 10,000 more
than sixty years after the Norwegian colonisation. In 1783 the total fell to 47,287, and in 1786 to 38,142
(Preyer and Zirkel, p. 483). Since the beginning of the present century we have exact and minute
computations:
Statistisk Tabel-Værk.
S. Qr. W. Qr. N. & B. Qrs. Total.
In A.D. 1801, 17,160 13,976 16,104 46,240(47,207?)
” 1806 (Preyer and Zirkel, whereas Mackenzie assigns it to 1804), 46,349
” 1808 (Preyer and Zirkel; and Mackenzie, p. 280), 48,063
” 1834, (Dillon, unofficial, evidently “round numbers”) 53,000
” 1835, 20,292 14,480 21,263 56,035
” 1840, 20,677 14,665 21,752 57,094
” 1842 (Meddel., ii. 70), 53,000
” 1845, 21,364 14,956 22,238 58,358

Skýrslur.
S. Qr. W. Qr. N. & E. Qrs. Total.
In A.D. 1850, 21,288 15,112 22,757 59,157
” 1855, 22,810 16,362 25,431 64,603
” 1857 (Preyer and Zirkel), 66,929
” 1858 (Do.), 67,847
” 1860, 23,137 16,960 26,890 66,987
” 1865 (Vice-Consul Crowe), 68,000
” 1870, 25,063 17,001 27,699 69,763
” 1872 (estimated), 70,000
while that of Madeira is 80,000.
The following table (Skýrslur um landshagi á Íslandi, v. 310, 1872) shows the increase of
population during the present century down to 1870:

From Feb. 1, 1801, to Feb. 2, 1835, increase 18·71 per cent.


” Feb. 2, 1835, to Nov. 2, 1840, ” 1·89 ”
” Nov. 2, 1840, to Nov. 2, 1845, ” 2·55 ”
” Nov. 2, 1845, to Feb. 1, 1850, ” 1·01 ”
” Feb. 1, 1850, to Oct. 1, 1855, ” 9·21 ”
” Oct. 1, 1855, to Oct. 1, 1860, ” 3·69 ”
” Oct. 1, 1860, to Oct. 1, 1870, ” 4·14 ”

The average rate of increase during the last century was very small: between A.D. 1703 and 1758 it
was about one-fifth of 1 per cent. During the present age there has been, we observe, a tolerably regular
progress with only three exceptions (A.D. 1835-40, A.D. 1845-50, and A.D. 1860-70). During this decade
(1860-70) there has been a considerable failure, 4·14 per cent., or only 2·05 for each lustrum. In 1872,
as will be seen, the number of males was 33,102; of females, 36,660. But throughout Iceland the
fluctuations have ever been so great as to reduce the value of “general considerations.”

The following tables are compiled from the minute returns made to the Danish Government, and
published in vols. i.-vi· of 1852-61, of the Meddelser fra det Statistishe Bureau, Copenhagen.
No. 1.—Table showing the Population of Iceland and its Distribution on the 1st February 1850, and
on the 1st October 1855.

No. of Families. Population. Increase,


Districts.
1850. 1855. 1850. 1855. in hundredths.
Southern Amt.
Reykjavik, 219 250 1149 1354 17·84
Gullbríngu and Kjósar Sýsla,{*} exclusive of Revkjavik, 783 853 4519 4853 7·39
The same, including Reykjavik, 1002 1103 5668 6207 9·51
Borgarfjarðar Sýsla, 329 355 2097 2312 10·25
Árnes Sýsla, 723 755 5018 5382 7·25
Rángárvalla Sýsla, 700 717 4766 4917 3·17
Austr and Vestr Skaptafells Sýsla,{*} 481 529 3340 3545 6·14
Vestmannaeyja{**} Sýsla, 91 98 399 447 12·03
Total (Southern Amt), 3326 355721,288 22,810 7·15

Western Amt.
Mýra and Hnappadals Sýsla,{*} 379 383 2410 2569 6·60
Snæfellsness Sýsla, 512 526 2684 2825 5·25
Dala Sýsla, 267 277 1923 2104 9·41
Barðastrandar Sýsla, 336 347 2518 2703 7·35
Isafjarðar{***} Sýsla, 508 545 4204 4589 9·16
Stranda Sýsla, 179 190 1373 1572 14·49
Total (Western Amt), 2181 2268 15,112 16,362 8·27

Northern And Eastern Amts.


Húnavatns Sýsla, 556 639 4117 4637 12·63
Skagafjarðar Sýsla, 626 622 4033 4258 5·58
Eyjafjarðar Sýsla, 625 638 3965 4289 8·17
Norðr and Suðr Thingeyjar Sýsla,{*} 640 684 4453 5108 14·71
Norðr-Múla Sýsla, 405 473 3201 3754 17·28
Suðr-Múla Sýsla, 391 416 2988 3385 13·29
Total (Northern and Eastern Amts), 3243 347222,757 25,431 11·75
Total for all Iceland, 8750 929759,157 64,603 9·21

{*} Separated on Ólsen’s map.


{**} Apparently combined with Rángárvalla Sýsla on Ólsens map.
{***} Sub-divided into north and west by P. and Z., p. 480; Mck., p. 281.
No. II.—Distribution of the Population of Iceland according to ages in 1855.
Ages. Per cent. Ages. Per cent.
Under 20 years, 42·315 Between 50 and 60 years, 9·303
Between 20 and 30 years, 19·485 Between 60 and 70 years, 5·413
Between 30 and 40 years, 11·886 Over 70 years, 2·463
Between 40 and 50 years, 9·135

No. III.—Table showing the Means of Support of the Population of Iceland on the 1st October 1855.
PROVIDING SUPPORTED. Percentage
TOTAL.
OCCUPATIONS. SUPPORT. Wives & Families. Servants. of
Males. Females. Total. Males. Females. Total. Males. Females. Total. Males. Females. Population. Total.
Ecclesiastics and
196 7 203 399 623 1022 527 613 1140 1122 1243 2365 3·66
teachers,
Civil officials
45 2 47 74 105 179 105 123 228 224 230 454 0·70
and employés,
Persons who live
84 89 170 40 84 124 18 44 62 139 217 356 0·55
on their means,
Men of science
29 — 29 20 42 62 20 29 49 69 71 140 0·22
and letters,
Persons who live
7063 618 7681 11,835 19,354 31,189 6112 7493 13,605 25,010 27,465 52,475 81·23
by agriculture,
Persons who live
980 86 1066 1090 1925 3015 465 509 974 2535 2520 5055 7·82
by the sea,
Mechanics, 199 27 226 133 219 352 59 73 132 391 319 710 1·10
Traders and inn-
87 4 91 136 231 367 117 155 272 340 390 730 1·13
keepers,
Persons who
172 62 234 97 168 265 13 11 24 282 241 523 0·81
work by the day,
Others who
pursue no
162 123 285 67 172 239 20 42 62 249 337 586 0·91
definite
occupation,
Receiving alms, 497 710 1207 — — — — — — 497 710 1207 1·87
Prisoners, 2 — 2 — — — — — — 2 — 2 0·00
Total, 9513 1728 11,241 13,891 22,923 36,814 7456 9092 16,548 30,860 33,743 64,603 100·00
Percentage of
14·7 2·7 17·4 21·5 35·5 57·0 11·5 14·1 25·6 47·8 52·2 100·0 —
population,

The following are the latest returns:


Table showing the Population of Iceland on the 1st October 1860 and 1870.
Increase
Number Population.
and
Districts. of
Decrease
Families. 1860. 1870.
per cent.

Southern Amt.
Reykjavik, 356 1444 2024
Gullbríngu and Kjósar Sýsla, 824 5001 5302 + 13·7
Borgarfjarðar Sýsla, 352 2251 2590 + 15·1
Árnes Sýsla, 772 5409 5891 + 8·9
Rángárvalla Sýsla, 689 5034 5201 + 3·3
Austr and Vestr Skaptafells Sýsla, 490 3499 3484 - 0·4
Vestmannaeyja Sýsla, 885 499 571 + 14·4
Total (Southern Amt), 3568 23,137 25,063 + 8·3

Western Amt.
Mýra and Hnappadals Sýsla, 373 2663 2765 + 3·9
Snæfellsness Sýsla, 471 2869 2799 - 2·4
Dala Sýsla, 285 2223 2190 - 1·5
Barðastrandar Sýsla, 311 2727 2699 - 1·0
Ísafjarðar Sýsla, 518 4860 4895 + 0·7
Stranda Sýsla, 192 1618 1653 + 2·2
Total (Western Amt), 2150 16,960 17,001 + 0·2

Northern and Eastern Amts.


Húnavatns Sýsla, 623 4722 4906 + 3·9
Skagafjarðar Sýsla, 614 4379 4574 + 4·5
Eyjafjarðar Sýsla, 707 4647 5108 + 9·9
Thingeyjar Sýsla, 715 5497 5746 + 4·5
Norðr-Múla Sýsla, 487 4183 3885 + 0·5
Suðr-Múla Sýsla, 442 3462 3480 - 7·1
Total (Northern and Eastern Amts), 3588 26,890 27,699 + 3·0
Total for all Iceland, 9306 66,987 69,763 + 4·1

The following is the official list of households for 1872:

In the Suðr-umdæmið (South Quarter) are 3568 households, with 11,835 men and 13,228 women.
” Vestr ” (West ” ) ” 2150 ” 7,981 ” ” 9,019 “
” Norðr og Austr ” 3588 ” 13,286 ” ” 14,413 “
Total, 9306 33,102 men and 36,660 women.

According to Mr Vice-Consul Crowe (Report), during the average of ten years (1855-65) there was
annually—

1 marriage for every 143 persons.


1 birth for every 25 “
1 death for every 39 “
1 deaf and dumb for every 994 “
1 blind 320 “

In 1855 there were 202 blind and 65 born surd-mutes. In 1870 the former numbered 225 (160 men
and 65 women), and the latter 50 (20 + 30).
In table III. (1855), we see that of 64,603 souls, 52,475, about three-fourths of the heads of families
and those who provide support, lived by farming, that is, by cattle-breeding, whilst more than four-fifths
of the entire population thus derived their maintenance. At the same time, 5055 were fishermen, and
only 703 were traders, showing a primitive state of society. Mr Consul Crowe (Report, 1870-71)
remarks: “Somewhat more than the 75 per cent. of the total population were engaged in sheep rearing
and agricultural pursuits; and, notwithstanding the steady and lucrative nature of the fisheries, only
about 10 per cent. were engaged in them.” The mechanics may be further distributed as follows:
Bakers, (in 1855) 1 proportion per thousand 0·01 in 1870 numbered 2
Coopers, ” 35 ” 0·55 ” 17
Gold & Silver Smiths, ” 80 ” 1·24 ” 21
Blacksmiths, ” 80 ” 1·24 ” 31
Carpenters, ” 61 ” 0·94 ” 12
Masons, ” 6 ” 0·09 ” 2
Millers, ” 4 ” 0·07 ” 1
Turners, ” 8 ” 0·13 ” 1
Boatbuilders, ” 38 ” 0·59 ” 12
Tailors, ” 27 ” 0·41 ” 10
Joiners, ” 174 ” 2·69 ” 56
Saddlers, ” 46 ” 0·71 ” 15
Weavers, ” 20 ” 0·30 ” 4
Watchmakers, ” 0 ” 0·00 ” 1
Other industries, ” 103 ” 1·59 ” 24

The following is a table of ages in 1870:


MEN WOMEN.
Years. Married. Unmarried. Widowers. Separated. Married. Unmarried. Widows. Separated.
1 801 777
1-2 1530 1570
3-4 1814 1798
5-6 1828 1768
7-10 3073 3090
11-15 3713 3715
16-20 3 3693 39 3706
21-25 143 2374 2 350 2301 14 3
26-30 843 1601 16 7 1031 1691 55 9
31-35 1224 814 44 12 1384 1046 126 17
36-40 1869 650 96 17 1867 916 226 31
41-45 1377 307 107 18 1225 523 289 22
46-50 1125 171 131 29 1067 350 343 23
51-55 751 100 114 17 623 232 361 24
56-60 501 83 111 4 456 204 359 10
61-65 424 67 154 6 360 203 383 9
66-70 341 64 208 7 282 208 494 8
71-75 178 42 174 2 130 113 346 3
76-80 70 10 126 1 50 60 206
81-85 28 3 54 16 24 88 1
86-90 5 1 12 7 15
91-95 1 1 1 2 5
96-100 1 2 3
Above 100[165] none. none.
8882 22,740 1361 120 888 24,306 3313 160
| |
33,103 36,660
|
69,763

According to Mr Consul Crowe (Report, 1870-71), the proportion between births and deaths was:

Year. Births. Deaths. Computed Population. Percentage.


1861 2525 2391 66,973 + 0·20
1862 2693 2874 66,792 + 0·27
1863 2648 2115 67,325 + 0·80
1864 2760 2001 68,084 + 1·13
1865 2757 2100 68,741 + 0·96
1866 2662 3122 68,281 + 0·67
1867 2743 1770 69,254 + 1·42
1868 2449 1970 69,733 + 0·69
1869 2177 2404 69,506 + 0·33
1870 2276 1698 70,084 + 0·83
Total, 25,690 22,445

The tables of 1855 gave an excess of 2865 women. Mackenzie (1801) shows 21,476 males to
25,731 females, or 4255 out of a total of 47,207. In 1865 the proportion of men to women was 1000:
1093. In 1870 the conditions had improved, the surplus being only 3554 out of 69,763, a small
percentage of waste labour.
It is easy to account for the preponderance of women, as well as their superior longevity, without
entering into the knotty subject of what determines sex. They lead more regular lives, they have less
hardship and fatigue, and they are rarely exposed to such accidents as being lost at sea or “in the mist.”
According to Mr Vice-Consul Crowe, in 1865-66, of every forty-two deaths one was by drowning.
There is a tradition that Iceland during its palmiest days contained 100,000 souls, but it seems to rest
upon no foundation. On the other hand, the old superstitious belief that some fatal epidemic invariably
follows an increase beyond 60,000, has, during the last few years, shown itself to be equally groundless.
It is probably one of the post hoc, ergo propter hoc confusions so popular amongst the vulgar; and,
unhappily, not confined to the vulgar.

§ 2. General Considerations.

“The first inhabitants of the northern world, Dania, Nerigos, and Suæcia,” says Saxo Grammaticus,
repeated by Arngrímr Jónsson, “were the posterity and remnant of the Canaanites quos fugavit Jesus
latro—expulsed from Palestine about A.C. 1500 by Joshua and Caleb.” Duly appreciating the
ethnological value of this tradition, we may remark that the occupation of Ultima Thule, which the
ancients evidently held to be inhabited—tibi serviat must mean that there were men to serve—has not
yet been proved. But Mongoloid or præ-Aryan colonies in ancient days seem to have overrun all the
Old, if not the New World, and we must not despair of tracing them to Iceland.
The modern Icelander is a quasi-Norwegian, justly proud of the old home. His race is completely
free from any taint of Skrælling, Innuit,[166] or Mongoloid blood, as some travellers have represented,
and as the vulgar of Europe seem to believe. Here and there, but rarely, a dark flat face, oblique eyes,
and long black horsehair, show that a wife has been taken from the land
“Where the short-legged Esquimaux
Waddle in the ice and snow.”

In the southern parts of the island there is apparently a considerable Irish


infusion; and we often remark the “potato face” and the peculiar eye, with
grey-blue iris and dark lashes so common in outer Galway, and extending to
far Tenerife.
It has been the fashion for travellers to talk of “our Scandinavian
ancestors in Iceland,” to declare that the northern element is the “backbone
of the English race,” and to find that Great Britain owes to the hyperborean
“her pluck, her go-ahead, and her love of freedom.”
That a little of this strong liquor may have done abundant good to the
puerile, futile Anglo-Kelt, and the flabby and phlegmatic Anglo-Saxon,
there is no doubt, but happily we have not had a drop too much of northern
blood. The islanders are by no means slow to claim descent from the old
Jarls of Norway and Sweden, whilst some of the peasantry have asserted,
and, it is said, have proved, consanguinity with the Guelphs: this would
make them Germans, like the Royal Family of Denmark, who enjoy only
poetical and laureated connection with the “Sea-Kings.” Those who reject
these pretensions reply that every noble house emigrating from Scandinavia
in the ninth and tenth centuries brought with it a train of serfs and thralls;
for instance, Njál headed nearly thirty fighting men, serviles included, and
Thráin led fifteen house-carls trained to arms. And genealogical statistics
prove that while the Jarl’s blood dies out, the Carl’s increases and
multiplies.
The Saga’s description of Gunnar Hamondsson is that of a well-favoured
Icelander in the present day: “He was handsome of feature and fair-skinned;
his nose was straight and a little turned up at the end (‘tip-tilted’); he was
blue-eyed, and bright-eyed, and ruddy cheeked; his hair was thick and of
good hue, and hanging down in comely curls.” And Skarphèdinn Njálsson
may stand forth as the typical “plain” Thulite: “His hair was dark-brown,
with crisp curly locks; he had good eyes; his features were sharp, and his
face ashen pale; his nose turned up, and his front teeth stuck out, and his
mouth was very ugly.”
The Icelander’s temperament is nervoso-lymphatic, and, at best,
nervoso-sanguineous. The nervoso-bilious, so common in the south of
Europe, is found but rarely; and the author never saw an instance of the
pure nervous often met with in the United States and the Brazil. The shape
of the cranium is distinctly brachycephalic, like the Teuton who can almost
always be discovered by his flat occiput and his projecting ears. The face is
rather round or square than oval; the forehead often rises high, and the
malar bones stand out strongly, whilst the cheeks fall in. A very
characteristic feature of the race whose hardness, not to say harshness, of
body and mind still distinguishes it from its neighbours, is the eye, dure and
cold as a pebble—the mesmerist would despair at the first sight. Even
amongst the “gentler sex” a soft look is uncommonly rare, and the aspect
ranges from a stoney stare to a sharp glance rendered fiercer by the habitual
frown. Hence probably Uno Von Troil (p. 87) describes the women as
generally ill-featured. The best specimens are clear grey or light blue, rarely
brown and never black; and the iris is mostly surrounded by a ring of darker
colour, the reverse of arcus senilis. Squints and prominent eyeballs, in fact
what are vulgarly called “goggle eyes,” are common; and even commoner,
perhaps, are the dull colourless organs which we term “cods’ eyes.” The
“Irish eye,” blue with dark lashes, is still found in the southern part of the
island, where, perhaps, thralls’ blood is most common. A mild and chronic
conjunctivitis often results from exposure to sun-glare after dark rooms and
from reading deep into the night with dim oil lamps. The nose is seldom
aquiline; the noble and sympathetically advancing outlines of the
Mediterranean shores will here be sought for in vain.[167] The best are the
straight, the worst are offensive “pugs.” Only in two instances, both of them
men of good blood, I saw the broad open brows, the Grecian noses, the
perpendicular profiles, the oval cheeks, and the chins full, but not too full,
which one connects in idea with the Scandinavian sea-king of the olden day.
As a rule, then, the Icelandic face can by no means be called handsome.
The oral region is often coarse and unpleasant. Lean lips are not so
numerous as the large, loose, fleshy, and bordés or slightly everted, whilst
here and there a huge mouth seems to split the face from ear to ear. The
redeeming feature is the denture. The teeth are short, regular, bright-
coloured, and lasting, showing uncommon strength of constitution. They are
rarely clean when coffee and tobacco are abused, and they are yet more
rarely cleaned. Doubtless a comparatively scanty use of hot food tends to
preserve them. The jowl is strong and square, and the chin is heavy, the
weak “vanishing” form being very uncommon. The beard is sometimes
worn, but more often clean shaved off; it seldom grows to any length,
though the mustachios, based upon a large and solid upper lip, are bushy
and form an important feature. Thick whiskers are sometimes seen, and so
are “Newgate frills,” from which the small foxy features stand sharply out.
The other strong points are the skin and hair. The former is almost
always rufous, rarely milanous, and the author never saw a specimen of the
leucous (albino). The “positive blonde” is the rule; opposed to the negative
or washed out blonde of Russia and Slavonia generally. The complexion of
the younger sort is admirably fresh, pink and white; and some retain this
charm till a late age. Its delicacy subjects it to sundry infirmities, especially
to freckles, which appear in large brown blotches; exposure to weather also
burns the surface, and converts rose and lily to an unseemly buff and brick-
dust red. It is striated in early middle-age with deep wrinkles and it
becomes much “drawn,” the effect of what children call “making faces” in
the sunlight and snow-blink. In the less wholesome parts of the island the
complexion of the peasantry is pallid and malarious.
Harfagr (Pulchricomus) is an epithet which may apply to both sexes. The
hair, which belongs to the class Lissótriches, subdivision Euplokomoi, of
Hæckel and Müller (Allg. Ethnographie, 1873), seldom shows the darker
shades of brown; and in the very rare cases where it is black, there is
generally a suspicion of Eskimo or Mongoloid blood. The colour ranges
from carrotty-red to turnip-yellow, from barley-sugar to the blond-cendré so
expensive in the civilised markets. We find all the gradations of Parisian art
here natural; the “corn-golden,” the blonde fulvide, the incandescent
(“carrotty”), the flavescent or sulphur-hued, the beurre frais, the fulvastre or
lion’s mane, and the rubide or mahogany, Raphael’s favourite tint. The
abominable Hallgerda’s hair is the type of Icelandic beauty; it was “soft as
silk and so long that it came down to her waist.” Seldom straight and lank,
the chevelure is usually wavy, curling at the ends, when short cut, as in
England. The women have especially thick locks, which look well without
other art but braiding, and many of the men have very bushy hair. As in the
negro, baldness does not appear till a late age, and perhaps the Húfa (cap)
by exposing the larger part of the surface acts as a preservative; old men
and women, though anile beauty is very rare, are seen with grey and even
white locks exceptionally thick. Canities comes on later than in Scotland
and Sweden, yet scant attention is paid to the hair beyond washing at the
brook. The body pile is as usual lighter coloured.
The figure is worse than the face, and it is rendered even more uncouth
by the hideous swathing dress. The men are remarkable for “champagne-
bottle (unduly sloping) shoulders,” “broad-shouldered in the backside,” as
our sailors say. They are seldom paunchy, though some, when settled in
warmer climates, develop the schöne corpulenz of the Whitechapel sugar-
baker. They have the thick, unwieldy trunks of mountaineers, too long for
the lower limbs—a peculiarity of hill-men generally, which extends even to
the Bubes of Fernando Po. The legs are uncommonly sturdy; the knees are
thick and rounded, an unpromising sign of blood; the ankles are coarse, and
the flat feet are unusually large and ill-formed, like the hands, a point of
resemblance with the Anglo-Saxon pure and simple. Hence they are
peculiarly fitted for their only manly sport, besides skating and shooting,
“Glímu list:” this wrestling has a “chic” of its own, though very different
from the style of Cumberland and Cornwall. The gait, a racial distinction, is
shambling and ungraceful, utterly unlike the strut of Southern Europe and
the roll of the nearer East; the tread is ponderous, and the light fantastic toe
is unknown. This “wabble” and waddle result from the rarity of walking-
exercise compared with riding and boating, and from the universal use of
the seal-skin slipper. The habit becomes a second nature: all strangers
observe the national trick of rocking the body when sitting or standing to
talk, and they mostly attribute it to the habit of weaving, when it is practised
by thousands who never used a loom. The feminine figure is graceful and
comparatively slender in youth, like the English girl of the “willowy type,”
but the limbs are large and ungainly. After a few years the “overblown”
forms broaden out coarsely. Women do not draw the plough, as in Greece
and parts of Ireland, but they must take their turn at all manner of field-
work. The Frauen-cultus, said to be a native of Europe north of the Alps,
has not extended here, at least in these days.[168] Hence the legs and ankles,
hands and feet, rival in size and coarseness those of the men. As wives, they
would be efficient correctives to the “fine drawn” framework and the over-
nervous diathesis of southern nations. Cold in temperament, they are
therefore, like the Irish, prolific, which may also result from the general
fish-diet. Dr Schleisner, who resided in Iceland under the Danish
Government, has proved the temperature of the blood to be higher than
amongst other races. Assuming the average of Europeans at C. 36°·5 (= F.
97°·7), nine persons out of twelve exceeded C. 37° (= F. 98°·6): the
maximum was C. 37°·8 (= F. 100°); the minimum was C. 36°·5, and the
average was C. 37°·27 (= F. 99°·09).[169]
Intermarriage is so general that almost all the chief families are cousins;
yet among several thousands the author saw only one hunchback, two short
legs, and a few hare-lips. It is almost needless to say that the common
infanticide of pagan days is now unknown, and that we must seek some
other cause for the absence of deformity. It may be found, perhaps, in the
purity of unmixed blood, which, mentioning no other instances, allows
consanguineous marriages to the Jews, the Bedawin Arabs, and even to the
Trasteverino Romans;[170] whereas composite and heterogeneous races like
the Englishman, the Spaniard, and especially the New Englander, cannot
effect such unions without the worst results—idiocy and physical deformity.
As regards uncleanliness in house and body, it may be said that the
Icelander holds a middle rank between the Scotchman and the Greenlander,
and he contrasts badly with the Norwegian of modern days. Personal purity,
the one physical virtue of old age, is, as a rule, sadly neglected. Concerning
this unpleasant topic, the author is compelled to offer a few observations.
The old islander could rival the seal: his descendant, like the man of Joe
Miller, will not trust himself in water before he can swim. The traveller
never sees man or woman in sea, river, or brook, though even the lower
animals bathe in hot weather. It is a race abominantes aquam frigidam, and,
even as pagans, their chief objection to Christianity was the necessity of
baptism: they compounded for immersion in the Laug or hot spring,[171]
and the latter is still, though very seldom, used. Washing is confined to the
face and hands; and the tooth-brush is unknown like the nail-brush: the
basins, where they exist, are about the size of punch-bowls. Purification by
water, after Moslem fashion, is undreamed of. Children are allowed to
contract hideous habits, which they preserve as adults; for instance, picking
teeth, and not only teeth, with dinner-forks. Old travellers, who perhaps had
not observed the cellarman in the wine vaults (London Docks) bore a hole
and blow through it to start the liquor, record a peculiarly unpleasant
contrivance for decanting the milk-pan into narrow-necked vessels; the
same, in fact, adopted by the Mexican when bottling his “Maguey;” and
“Blefkenius” alludes to a practice still popular amongst the Somal: it is only
fair to own that the author never saw them. The rooms, and especially the
sick rooms, are exceedingly stifling and impure. Those who venture upon
an Icelandic bed may perhaps find clean sheets, but they had better not look
under them. The houses, except in the towns, or the few belonging to
foreign merchants, have no offices, and all that have, leave them in a
horrible condition: there is no drainage, and the backyard is a mass of offal.
Such is the effect of climate, which makes dirt the “poor man’s jacket” in
the north; which places cleanliness next to godliness in the sub-tropical
regions, and which renders personal uncleanliness sinful and abominable to
the quasi-equatorial Hindú. Nor must we forget that the old English proverb
“Washing takes the marrow out of a man,” still has significance amongst
our peasantry.

§3. Character.

Appreciations of national character too often depend upon the casual


circumstances which encounter and environ the traveller; and writers upon
Iceland differ so greatly upon the matter, that perhaps the safest plan will be
to quote the two extremes.
The unfriendly find the islanders serious to a fault; silent, gloomy, and
atrabilious; ungenial and morose; stubborn and eternally suspicious;
litigious and mordant; utterly deficient in adventure, doing nothing but what
necessity compels; little given to hospitality; greedy of gain, and
unscrupulous in the quocumque modo rem. “Gaiety,” says one, “seems
banished from their hearts, and we should suppose that all are under the
influence of that austere nature in the midst of which they were born.”
Henderson (i. 34), who represents the bright side of the picture, enlarges
upon their calm and dignified, their orderly and law-abiding character; he
denies their being of sullen and melancholy disposition; he was surprised at
the degree of cheerfulness and vivacity prevailing among them, and that,
too, not unfrequently under circumstances of considerable external
depression. They are so honest that the doors are not locked at night in their
largest town; strangely frank and unsophisticated; ardent patriots and lovers
of constitutional liberty; fond of literature, pious, and contented; endowed
with remarkable strength of intellect and acuteness; brimful of hospitality,
and not given to any crimes, or indeed vices, except drunkenness.
And, upon the principle of allowing the Icelander to describe himself, we
may quote as an exemplar of character the following model epitaph: “To the
precious memory of A., S.’s son, who married the maiden C., D.’s daughter.
He was calm in mind; firm in council; watchful, active, his friends’ friend;
hospitable, bountiful, upright towards all, and the affectionate father of his
house and children.”
The truth is, that although isolation has, as might be expected, preserved
a marked racial character, the islandry are much like other Northmen.
During the pagan times, and indeed until the sixteenth century, we read
“their chief characteristics were treachery, thirst for blood, unbounded
licentiousness, and inveterate detestation of order and rule;” but we shall
hardly recognise the picture now. They are truthful, and they appear pre-
eminently so to a traveller from the south of Europe, or from the Levant.
They have a sense of responsibility, and you may believe their oaths: at the
same time, they look upon all men as liars, and they are as desconfiados
(distrustful) as Paulistas or Laplanders—a mental condition apparently
connected with a certain phase of civilisation. Compared with the sharp-
witted Southron, they are dull and heavy, stolid and hard of comprehension
as our labouring classes, without the causes which affect the latter. They
cleave like Hindús to the father-to-son principle, and they have little at
home that tempts either to invention, to innovation, or to adventure. They
are a “polypragmatic peasantry;” the love of lawsuits still distinguishes the
Norman in France after ages of separation from the parent stock. Even in
private debate they obstinately adhere to the letter, and shun the spirit: an
Icelander worsted in argument takes up some verbal distinction or
secondary point, and treats it as if it were of primary importance. An
exaggeration of this peculiarity breeds the Querelle d’Allemand.
Another peculiarity of the islandry is a bitterly satirical turn of mind, a
quality noted of old. We rarely meet with a “Thorkel Foulmouth,” but we
see many a Skarphèdinn who delights and who takes pride in dealing those
wounds of the tongue which according to the Arabs never heal. An ancient
writer gives a fair measure of what could be done by Níðvísur[172]
(lampoons), which never spared even the kings. They threatened Harold the
Dane to write as many lampoons upon him as there were noses[173] in
Iceland (Ólaf Tryggvason’s Saga, xxxvii.), and escaped by magic from an
invasion. Nor did they spare even the gods; for instance, Hjalti sings (Burnt
Nial’s Saga):
“I will not serve an idle log,
For one, I care not which;
But either Odin is a dog
Or Freya is a——.”

The term “Tað-skegglingar,” Dung-beardlings, applied by a woman to


certain youths whom she hated, caused a small civil war. When Dr Wormius
was Rector Magnificus of the Copenhagen University, an Icelandic student
complained of a libellous fellow-countryman. The poet, when summoned,
confessed the authorship; contended that it contained no cause of offence,
and, with characteristic plausibility and cunning, talked over the simple
Vice-Chancellor. Thereupon the plaintiff in tears told the Rector that his fair
fame was for ever lost, explaining at the same time the “fables, figures, and
other malicious designs under which the malignity of the satire was
couched;” and even the “spells and sorceries” which threatened his life.
Thereupon Dr Wormius took high ground, and by citing certain severe laws
against witchcraft, persuaded the poet to tear up his satire and never to write
or to speak of it again. “The student was ravished with joy,” because he had
made his peace with a pest who could exceed in power of annoyance
Aristophanes, Horace, and Juvenal.
The courage, steadfastness, and pertinacity of the Icelander are proved
by his annals, and if he does not show these qualities in the present day, it is
because they are overlaid by circumstances. As regards the relations of the
sexes, we find nothing in the number of illegitimate children which justifies
the poet in singing of the “moral north.”[174] Iceland in fact must be
reckoned amongst the

“Littora quæ fuerunt castis inimica puellis;”

and although she has improved upon the reckless licentiousness of the Saga
days, ichthyophagy and idleness must do much to counterbalance the “sun-
clad power of chastity.” The “unsophistication” of the race is certainly on
the wane; there are doubtless

“Honest men from Iceland to Barbadoes,”


but the islander is pre-eminent for a “canniness” which equals, if it does not
exceed, that of the Yankee, the lowland Scotch, and the Maltese. And what
he gains he can keep with a most tenacious hold.
The statistics of crime in Iceland are peculiarly unsatisfactory. As the
Journal will show, many a man goes free who would be prosecuted and
severely punished farther south. Traveller after traveller has asserted, “it is
in a large measure to their widespread home education that we must
attribute the fine moral character of the Icelanders;” and capital has been
made of the fact that the old stone-prison became the Government House.
The Danish Parliamentary Reports (p. 255, vol. xlvii. for 1837-1838)
contain details concerning the number of persons arraigned and convicted,
sentenced, and acquitted by the tribunals. During a period of seven years
(1827-1834), there were but 292 indictments on the island; of these 216
ended in conviction; 20 cases were suspended; 32 were dismissed, and 56
were acquitted. Of the 216 convictions, 79 were for “carnal offences;” 86
for larceny; 15 for transgressing sanitarial laws; 5 for murder, and 31 for
various offences, such as false-witness and receiving stolen goods. The last
statistics in 1868 give 46 criminal cases (37 males, 9 females) for the whole
island, and in 37 conviction and sentence followed; 34 were for theft, 1 for
forgery, 2 for adultery, besides 29 were fined for disturbance of the peace
and for offences against public order. There were also 57 cases of adultery
and seduction; 24 of these were fined, and in 33 cases the fine was remitted
(Skýrslur um Landshagi, v. 193, 1871).
The suicide,[175] arson, and infant exposure of the republican and pagan
ages are no longer heard of; vagrancy is hardly an offence; the state of the
country prevents technical robbery; and forgery does not belong to its
present state of civilisation. It is peculiar that almost all classes believe in
and fear a tribe of outlaws or bandits who occupy the deserts of the interior
—these are the days of Robin Hood come again.

§ 3. Society.

The social condition of Iceland has been compared with Lord


Macaulay’s pictures of the Highlanders a hundred and fifty, and of the
English three hundred years ago—the differences are more salient than the
points of resemblance. The proverb “Heimskt er heimaðlia barn” (homely is
the housebred child) produced a habit of voyaging and travelling; and wide
wandering made the homes centres of refinement: the same practice in the
Hebrides astonished Dr Johnson. Unhappily it is now no longer the popular
habit; it has gone the way of the manly exercises, bowls, quoits, swimming,
and practising weapons, which distinguished the heroic age. With much
aristocratic feeling there is no aristocratic order properly so called; the earl,
the baron, and the clan-chief are equally unknown; whilst the parson, like
the priest in Slavonic countries, is the modern pattern to the Thane or Churl.
As in the United States, there is no gentlemen class except the liberal
professions, and even the clergy until the present generation were farmers
and fishermen, labourers, mechanics, and so forth, often poorer and
shabbier than the laity. The official circles are too small to form a beamten-
kreis; the squirearchy is represented by the franklins or peasant lairds, who
no longer correspond with the ancient Udallers; the merchants are chiefly
foreigners.
Under these circumstances we can hardly expect much general
refinement, nor the particular phase which produces men whose life
consists in adorning society, and women born to wear diamonds and to be
beautiful. Yet the Icelander, franklin or pauper, has none of the roughness
and rudeness which we remark in the manners of the Canadians and of the
lowland Scotch. “No tax is levied upon civility,” and their mutual regard for
one another’s feelings, though sometimes carried to an inconvenient extent,
is the essence of true politeness. The intercourse is rather ceremonious than
“free and easy,” and travellers deride such quaint mixture as “You lie, my
blessed (or beloved) friend!” The abuse of mutual regard is a servile fear of
making enemies; they often tamely put up with injuries, as the Brazilian
submits to be plundered by a richer neighbour, and the Syrian swallows his
wrath rather than offend one who may some day become a Pasha.
The Icelander is a large-brained and strong-brained man, essentially slow
and solid in point of intellect, and capable of high culture, of wide learning,
and of deep research. This lesson is taught by the whole of his literature;
although the muse no longer sings of love and war, she is by no means mute
—her turn is now the theological, the philological, and the scientific.
Arngrímr Jónsson well describes his countrymen as “Ad totius Europæ res
historicas lyncæi.” But the islander never attains his full development
except out of his own country, and this condition dates from past ages.
Throughout the north, from England[176] and Val-land (France and Italy), to
Mikligarðr (Constantinople),[177] he has distinguished himself and proved

“That every country is a brave man’s home.”

Abroad, his emulation is excited, his ambition is roused, and his slow
sturdy nature is stirred up to unusual energy. At home he can command no
serious education, nor can he escape from the indolent and phlegmatic, the
dawdling and absolutely unconditioned slowness of the country, where time
is a positive nuisance, to be killed as it best can. In Iceland the author met
several Danes, but only two Icelanders, who spoke good English, French, or
German; it is far otherwise in Europe, and especially, we need not say, in
England.
As the notices of emigration will show, Iceland, like Ireland, is
instinctively seeking her blessing and salvation, the “racial baptism.” One
traveller records the “inexpressible attachment of the islanders for their
native country.” Their Sehn sucht in a mountainless land, and the time-
honoured boast, “Hið besta land solin skínr uppá” (Iceland is the best land
upon which the sun shines).[178] So Bjarni Thorarensen sings, “World-old
Iceland, beloved foster-land, thou wilt be dear to thy sons, as long as sea
girds earth, men love women, and sun shines on hills.” But all the people of
all the poorest countries console themselves in the same way, and
geographical ignorance confirms an idea which to the traveller becomes
simply ludicrous: moreover, northerners, it need hardly be said, gain more
by removal, and therefore emigrate more readily than southerners. The
latter express themselves unmistakably:

“Ἀνδρὶ γάρ τοι, κἄν ῦπερξάλλῃ κακοῖς


Οὐν ἔστι θρεψαντος ἤδιον πέδον.”

And “Ulysses ad Ithacæ suæ saxa properat, quemadmodum Agamemnon ad


Mycenarum nobiles muros; nemo enim patriam amat quia magna, sed quia
sua” (Seneca), They are happy at home; why should they leave home?
The Icelander cannot be called degenerate. He is what he was. But whilst
the world around, or rather beyond him, has progressed with giant strides,
he has perforce remained stationary. His mother country forbids him to
decuple the human hand and arm by machinery; the enormous water-power
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like