EloquentJavaScript

 Eloquent JavaScript

4th edition

Marijn Haverbeke

Copyright ? 2024 by Marijn Haverbeke

This work is licensed under a Creative Commons attribution-noncommercial

license (). All code in the

book may also be considered licensed under an MIT license (.

net/code/LICENSE).

The illustrations are contributed by various artists: Cover by P¨¦chane Sumie. Chapter illustrations by Madalina Tantareanu. Pixel art in Chapters 7 and

16 by Antonio Perdomo Pastor. Regular expression diagrams in Chapter 9

generated with by Jeff Avallone. Game concept for Chapter 16

by Thomas Palef.

You can buy a print version of this book, with an extra bonus chapter included,

printed by No Starch Press at .

i

Contents

Introduction

On programming . . . . .

Why language matters . .

What is JavaScript? . . . .

Code, and what to do with

Overview of this book . . .

Typographic conventions .

1

. .

. .

. .

it

. .

. .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Values, Types, and Operators

Values . . . . . . . . . . . . . . . . . .

Numbers . . . . . . . . . . . . . . . .

Strings . . . . . . . . . . . . . . . . .

Unary operators . . . . . . . . . . . .

Boolean values . . . . . . . . . . . . .

Empty values . . . . . . . . . . . . . .

Automatic type conversion . . . . . .

Summary . . . . . . . . . . . . . . . .

2 Program Structure

Expressions and statements

Bindings . . . . . . . . . . .

Binding names . . . . . . . .

The environment . . . . . .

Functions . . . . . . . . . . .

The console.log function . .

Return values . . . . . . . .

Control flow . . . . . . . . .

Conditional execution . . . .

while and do loops . . . . . .

Indenting Code . . . . . . .

for loops . . . . . . . . . . .

Breaking Out of a Loop . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

ii

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1

2

3

5

7

8

8

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

10

10

11

13

15

15

17

18

20

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

21

21

22

24

24

24

25

25

26

26

28

30

31

32

Updating bindings succinctly . . .

Dispatching on a value with switch

Capitalization . . . . . . . . . . . .

Comments . . . . . . . . . . . . . .

Summary . . . . . . . . . . . . . . .

Exercises . . . . . . . . . . . . . . .

3 Functions

Defining a function . . . .

Bindings and scopes . . . .

Nested scope . . . . . . . .

Functions as values . . . .

Declaration notation . . .

Arrow functions . . . . . .

The call stack . . . . . . .

Optional Arguments . . . .

Closure . . . . . . . . . . .

Recursion . . . . . . . . . .

Growing functions . . . . .

Functions and side effects

Summary . . . . . . . . . .

Exercises . . . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

4 Data Structures: Objects and Arrays

The weresquirrel . . . . . . . . . . . . . . . . . .

Datasets . . . . . . . . . . . . . . . . . . . . . .

Properties . . . . . . . . . . . . . . . . . . . . .

Methods . . . . . . . . . . . . . . . . . . . . . .

Objects . . . . . . . . . . . . . . . . . . . . . . .

Mutability . . . . . . . . . . . . . . . . . . . . .

The lycanthrope¡¯s log . . . . . . . . . . . . . . .

Computing correlation . . . . . . . . . . . . . .

Array loops . . . . . . . . . . . . . . . . . . . . .

The final analysis . . . . . . . . . . . . . . . . .

Further arrayology . . . . . . . . . . . . . . . . .

Strings and their properties . . . . . . . . . . .

Rest parameters . . . . . . . . . . . . . . . . . .

The Math object . . . . . . . . . . . . . . . . . .

Destructuring . . . . . . . . . . . . . . . . . . .

Optional property access . . . . . . . . . . . . .

iii

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

32

33

34

34

35

35

.

.

.

.

.

.

.

.

.

.

.

.

.

.

38

38

39

40

41

42

42

43

44

45

47

50

52

53

53

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

55

55

56

57

57

58

61

62

64

65

66

68

69

71

72

73

74

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download