Skip to content

Commit b270d60

Browse files
authored
Update files to current version (#426)
1 parent c8c2f1b commit b270d60

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+641
-556
lines changed

00_intro.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ could be written in English like this:
196196
8. Continue with instruction 3.
197197
9. Output the value of memory location 0.
198198

199-
{{index readability, naming, variable}}
199+
{{index readability, naming, binding}}
200200

201201
Although that is already more readable than the soup of bits, it is
202202
still rather obscure. Using names instead of numbers for the
@@ -244,7 +244,7 @@ console.log(total);
244244
// → 55
245245
```
246246

247-
{{index "while loop", loop, braces}}
247+
{{index "while loop", loop, [braces, block]}}
248248

249249
This version gives us a few more improvements. Most important, there
250250
is no need to specify the way we want the program to jump back and

02_program_structure.md

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ point where we can express meaningful prose.
2020

2121
## Expressions and statements
2222

23-
{{index grammar, syntax, [code, "structure of"], grammar, [JavaScript, syntax]}}
23+
{{index grammar, [syntax, expression], [code, "structure of"], grammar, [JavaScript, syntax]}}
2424

2525
In [Chapter ?](values), we made values and applied operators to them
2626
to get new values. Creating values like this is the main substance of
2727
any JavaScript program. But that substance has to be framed in a
2828
larger structure to be useful. So that's what we'll cover next.
2929

30-
{{index "literal expression"}}
30+
{{index "literal expression", [parentheses, expression]}}
3131

3232
A fragment of code that produces a value is called an
3333
_((expression))_. Every value that is written literally (such as `22`
3434
or `"psychoanalysis"`) is an expression. An expression between
35-
((parentheses)) is also an expression, as is a ((binary operator))
35+
parentheses is also an expression, as is a ((binary operator))
3636
applied to two expressions or a ((unary operator)) applied to one.
3737

3838
{{index [nesting, "of expressions"], "human language"}}
@@ -48,7 +48,7 @@ If an expression corresponds to a sentence fragment, a JavaScript
4848
_statement_ corresponds to a full sentence. A program is a list of
4949
statements.
5050

51-
{{index syntax}}
51+
{{index [syntax, statement]}}
5252

5353
The simplest kind of statement is an expression with a semicolon after
5454
it. This is a program:
@@ -82,9 +82,9 @@ about the subtleties of missing semicolons.
8282
## Bindings
8383

8484
{{indexsee variable, binding}}
85-
{{index syntax, [binding, definition], "side effect", memory}}
85+
{{index [syntax, statement], [binding, definition], "side effect", [memory, organization], [state, in binding]}}
8686

87-
How does a program keep an internal ((state))? How does it remember
87+
How does a program keep an internal state? How does it remember
8888
things? We have seen how to produce new values from old values, but
8989
this does not change the old values, and the new value has to be
9090
immediately used or it will dissipate again. To catch and hold values,
@@ -200,7 +200,7 @@ names—`catch22` is a valid name, for example—but the name must not
200200
start with a digit. A binding name may include dollar signs (`$`) or
201201
underscores (`_`) but no other punctuation or special characters.
202202

203-
{{index syntax, "implements (reserved word)", "interface (reserved word)", "package (reserved word)", "private (reserved word)", "protected (reserved word)", "public (reserved word)", "static (reserved word)", "void operator", "yield (reserved word)", "enum (reserved word)", "reserved word", [binding, naming]}}
203+
{{index [syntax, identifier], "implements (reserved word)", "interface (reserved word)", "package (reserved word)", "private (reserved word)", "protected (reserved word)", "public (reserved word)", "static (reserved word)", "void operator", "yield (reserved word)", "enum (reserved word)", "reserved word", [binding, naming]}}
204204

205205
Words with a special meaning, such as `let`, are _((keyword))s_, and
206206
they may not be used as binding names. There are also a number of
@@ -216,33 +216,35 @@ new package private protected public return static super
216216
switch this throw true try typeof var void while with yield
217217
```
218218

219+
{{index [syntax, error]}}
220+
219221
Don't worry about memorizing this list. When creating a binding produces
220-
an unexpected ((syntax error)), see whether you're trying to define a
222+
an unexpected syntax error, see whether you're trying to define a
221223
reserved word.
222224

223225
## The environment
224226

225-
{{index "standard environment"}}
227+
{{index "standard environment", [browser, environment]}}
226228

227229
The collection of bindings and their values that exist at a given time
228230
is called the _((environment))_. When a program starts up, this
229231
environment is not empty. It always contains bindings that are part of
230232
the language ((standard)), and most of the time, it also has bindings
231233
that provide ways to interact with the surrounding system. For
232-
example, in a ((browser)), there are functions to interact with the
234+
example, in a browser, there are functions to interact with the
233235
currently loaded website and to read ((mouse)) and ((keyboard)) input.
234236

235237
## Functions
236238

237-
{{indexsee "application (of functions)", "function application"}}
238-
{{indexsee "invoking (of functions)", "function application"}}
239-
{{indexsee "calling (of functions)", "function application"}}
240-
{{index output, function, [function, application]}}
239+
{{indexsee "application (of functions)", [function, application]}}
240+
{{indexsee "invoking (of functions)", [function, application]}}
241+
{{indexsee "calling (of functions)", [function, application]}}
242+
{{index output, function, [function, application], [browser, environment]}}
241243

242244
A lot of the values provided in the default environment have the type
243245
_((function))_. A function is a piece of program wrapped in a value.
244246
Such values can be _applied_ in order to run the wrapped program. For
245-
example, in a ((browser)) environment, the binding `prompt` holds a
247+
example, in a browser environment, the binding `prompt` holds a
246248
function that shows a little ((dialog box)) asking for user input. It
247249
is used like this:
248250

@@ -252,10 +254,10 @@ prompt("Enter passcode");
252254

253255
{{figure {url: "img/prompt.png", alt: "A prompt dialog", width: "8cm"}}}
254256

255-
{{index parameter, [function, application]}}
257+
{{index parameter, [function, application], [parentheses, arguments]}}
256258

257259
Executing a function is called _invoking_, _calling_, or _applying_
258-
it. You can call a function by putting ((parentheses)) after an
260+
it. You can call a function by putting parentheses after an
259261
expression that produces a function value. Usually you'll directly use
260262
the name of the binding that holds the function. The values between
261263
the parentheses are given to the program inside the function. In the
@@ -270,10 +272,10 @@ looks, but can be helpful in toy programs and experiments.
270272

271273
## The console.log function
272274

273-
{{index "JavaScript console", "developer tools", "Node.js", "console.log", output}}
275+
{{index "JavaScript console", "developer tools", "Node.js", "console.log", output, [browser, environment]}}
274276

275277
In the examples, I used `console.log` to output values. Most JavaScript
276-
systems (including all modern web ((browser))s and Node.js) provide a
278+
systems (including all modern web browsers and Node.js) provide a
277279
`console.log` function that writes out its arguments to _some_ text
278280
output device. In browsers, the output lands in the ((JavaScript
279281
console)). This part of the browser interface is hidden by default,
@@ -295,12 +297,12 @@ console.log("the value of x is", x);
295297

296298
if}}
297299

298-
{{index object}}
300+
{{index [object, property], [property, access]}}
299301

300302
Though binding names cannot contain ((period character))s,
301303
`console.log` does have one. This is because `console.log` isn't a
302304
simple binding. It is actually an expression that retrieves the `log`
303-
((property)) from the value held by the `console` binding. We'll
305+
property from the value held by the `console` binding. We'll
304306
find out exactly what this means in [Chapter ?](data#properties).
305307

306308
{{id return_values}}
@@ -366,7 +368,7 @@ control flow:
366368

367369
## Conditional execution
368370

369-
{{index Boolean, "control flow"}}
371+
{{index Boolean, ["control flow", conditional]}}
370372

371373
Not all programs are straight roads. We may, for example, want to
372374
create a branching road, where the program takes the proper branch
@@ -375,7 +377,7 @@ execution))_.
375377

376378
{{figure {url: "img/controlflow-if.svg", alt: "Conditional control flow",width: "4cm"}}}
377379

378-
{{index syntax, "Number function", "if keyword"}}
380+
{{index [syntax, statement], "Number function", "if keyword"}}
379381

380382
Conditional execution is created with the `if` keyword in JavaScript.
381383
In the simple case, we want some code to be executed if, and only if,
@@ -392,9 +394,11 @@ if (!Number.isNaN(theNumber)) {
392394

393395
With this modification, if you enter "parrot", no output is shown.
394396

397+
{{index [parentheses, statement]}}
398+
395399
The `if` keyword executes or skips a statement depending on the value
396400
of a Boolean expression. The deciding expression is written after the
397-
keyword, between ((parentheses)), followed by the statement to
401+
keyword, between parentheses, followed by the statement to
398402
execute.
399403

400404
{{index "Number.isNaN function"}}
@@ -405,9 +409,9 @@ function happens to return `NaN` when you give it a string that
405409
doesn't represent a valid number. Thus, the condition translates to
406410
"unless `theNumber` is not-a-number, do this".
407411

408-
{{index grouping, "{} (block)"}}
412+
{{index grouping, "{} (block)", [braces, "block"]}}
409413

410-
The statement after the `if` is wrapped in ((braces)) (`{` and
414+
The statement after the `if` is wrapped in braces (`{` and
411415
`}`) in this example. The braces can be used to group any number of
412416
statements into a single statement, called a _((block))_. You could
413417
also have omitted them in this case, since they hold only a single
@@ -481,7 +485,7 @@ console.log(10);
481485
console.log(12);
482486
```
483487

484-
{{index "control flow"}}
488+
{{index ["control flow", loop]}}
485489

486490
That works, but the idea of writing a program is to make something
487491
_less_ work, not more. If we needed all even numbers less than 1,000,
@@ -491,7 +495,7 @@ _((loop))_.
491495

492496
{{figure {url: "img/controlflow-loop.svg", alt: "Loop control flow",width: "4cm"}}}
493497

494-
{{index syntax, "counter variable"}}
498+
{{index [syntax, statement], "counter variable"}}
495499

496500
Looping control flow allows us to go back to some point in the program
497501
where we were before and repeat it with our current program state. If
@@ -509,15 +513,15 @@ while (number <= 12) {
509513
// … etcetera
510514
```
511515

512-
{{index "while loop", Boolean}}
516+
{{index "while loop", Boolean, [parentheses, statement]}}
513517

514518
A ((statement)) starting with the keyword `while` creates a loop. The
515-
word `while` is followed by an ((expression)) in ((parentheses)) and
519+
word `while` is followed by an ((expression)) in parentheses and
516520
then a statement, much like `if`. The loop keeps entering that
517521
statement as long as the expression produces a value that gives `true`
518522
when converted to Boolean.
519523

520-
{{index comparison, state}}
524+
{{index [state, in binding], [binding, as state]}}
521525

522526
The `number` binding demonstrates the way a ((binding)) can track the
523527
progress of a program. Every time the loop repeats, `number` gets a
@@ -550,7 +554,7 @@ but for reasons that will become apparent in [Chapter
550554
?](data#array_indexing), it is a good idea to get used to
551555
counting from 0.
552556

553-
{{index "loop body", "do loop", "control flow"}}
557+
{{index "loop body", "do loop", ["control flow", loop]}}
554558

555559
A `do` loop is a control structure similar to a `while` loop. It
556560
differs only on one point: a `do` loop always executes its body at
@@ -576,7 +580,7 @@ continues going round until you provide a non-empty name.
576580

577581
## Indenting Code
578582

579-
{{index "code structure", whitespace, "programming style"}}
583+
{{index [code, "structure of"], [whitespace, indentation], "programming style"}}
580584

581585
In the examples, I've been adding spaces in front of statements that
582586
are part of some larger statement. These spaces are not required—the computer
@@ -608,7 +612,7 @@ amount.
608612

609613
## for loops
610614

611-
{{index syntax, "while loop", "counter variable"}}
615+
{{index [syntax, statement], "while loop", "counter variable"}}
612616

613617
Many loops follow the pattern shown in the `while` examples. First a
614618
"counter" binding is created to track the progress of the loop. Then
@@ -631,16 +635,18 @@ for (let number = 0; number <= 12; number = number + 2) {
631635
// … etcetera
632636
```
633637

634-
{{index "control flow", state}}
638+
{{index ["control flow", loop], state}}
635639

636640
This program is exactly equivalent to the
637641
[earlier](program_structure#loops) even-number-printing example. The
638642
only change is that all the ((statement))s that are related to the
639643
"state" of the loop are grouped together after `for`.
640644

641-
The ((parentheses)) after a `for` keyword must contain two
645+
{{index [binding, as state], [parentheses, statement]}}
646+
647+
The parentheses after a `for` keyword must contain two
642648
((semicolon))s. The part before the first semicolon _initializes_ the
643-
loop, usually by defining a ((binding)). The second part is the
649+
loop, usually by defining a binding. The second part is the
644650
((expression)) that _checks_ whether the loop must continue. The final
645651
part _updates_ the state of the loop after every iteration. In most
646652
cases, this is shorter and clearer than a `while` construct.
@@ -714,7 +720,7 @@ iteration.
714720

715721
## Updating bindings succinctly
716722

717-
{{index assignment, "+= operator", "-= operator", "/= operator", "*= operator", state, "side effect"}}
723+
{{index assignment, "+= operator", "-= operator", "/= operator", "*= operator", [state, in binding], "side effect"}}
718724

719725
Especially when looping, a program often needs to "update" a binding
720726
to hold a value based on that binding's previous value.
@@ -747,7 +753,7 @@ equivalents: `counter++` and `counter--`.
747753

748754
## Dispatching on a value with switch
749755

750-
{{index syntax, "conditional execution", dispatching, ["if keyword", chaining]}}
756+
{{index [syntax, statement], "conditional execution", dispatch, ["if keyword", chaining]}}
751757

752758
It is not uncommon for code to look like this:
753759

@@ -782,7 +788,7 @@ switch (prompt("What is the weather like?")) {
782788
}
783789
```
784790

785-
{{index fallthrough, comparison, "break keyword", "case keyword", "default keyword"}}
791+
{{index fallthrough, "break keyword", "case keyword", "default keyword"}}
786792

787793
You may put any number of `case` labels inside the block opened by
788794
`switch`. The program will start executing at the label that
@@ -797,7 +803,7 @@ executed.
797803

798804
## Capitalization
799805

800-
{{index capitalization, [binding, naming], whitespace}}
806+
{{index capitalization, [binding, naming], [whitespace, syntax]}}
801807

802808
Binding names may not contain spaces, yet it is often helpful to use
803809
multiple words to clearly describe what the binding represents. These
@@ -1036,7 +1042,7 @@ Passing this string to `console.log` should show something like this:
10361042
```
10371043

10381044
When you have a program that generates this pattern, define a
1039-
((binding)) `size = 8` and change the program so that it works for
1045+
binding `size = 8` and change the program so that it works for
10401046
any `size`, outputting a grid of the given width and height.
10411047

10421048
{{if interactive
@@ -1052,7 +1058,7 @@ if}}
10521058
You can build the string by starting with an empty one (`""`) and
10531059
repeatedly adding characters. A newline character is written `"\n"`.
10541060

1055-
{{index [nesting, "of loops"], braces}}
1061+
{{index [nesting, "of loops"], [braces, "block"]}}
10561062

10571063
To work with two ((dimensions)), you will need a ((loop)) inside of a
10581064
loop. Put braces around the bodies of both loops to make it

0 commit comments

Comments
 (0)