@@ -20,19 +20,19 @@ point where we can express meaningful prose.
20
20
21
21
## Expressions and statements
22
22
23
- {{index grammar, syntax, [ code, "structure of"] , grammar, [ JavaScript, syntax] }}
23
+ {{index grammar, [ syntax, expression ] , [ code, "structure of"] , grammar, [ JavaScript, syntax] }}
24
24
25
25
In [ Chapter ?] ( values ) , we made values and applied operators to them
26
26
to get new values. Creating values like this is the main substance of
27
27
any JavaScript program. But that substance has to be framed in a
28
28
larger structure to be useful. So that's what we'll cover next.
29
29
30
- {{index "literal expression"}}
30
+ {{index "literal expression", [ parentheses, expression ] }}
31
31
32
32
A fragment of code that produces a value is called an
33
33
_ ((expression))_ . Every value that is written literally (such as ` 22 `
34
34
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))
36
36
applied to two expressions or a ((unary operator)) applied to one.
37
37
38
38
{{index [ nesting, "of expressions"] , "human language"}}
@@ -48,7 +48,7 @@ If an expression corresponds to a sentence fragment, a JavaScript
48
48
_ statement_ corresponds to a full sentence. A program is a list of
49
49
statements.
50
50
51
- {{index syntax}}
51
+ {{index [ syntax, statement ] }}
52
52
53
53
The simplest kind of statement is an expression with a semicolon after
54
54
it. This is a program:
@@ -82,9 +82,9 @@ about the subtleties of missing semicolons.
82
82
## Bindings
83
83
84
84
{{indexsee variable, binding}}
85
- {{index syntax, [ binding, definition] , "side effect", memory}}
85
+ {{index [ syntax, statement ] , [ binding, definition] , "side effect", [ memory, organization ] , [ state, in binding ] }}
86
86
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
88
88
things? We have seen how to produce new values from old values, but
89
89
this does not change the old values, and the new value has to be
90
90
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
200
200
start with a digit. A binding name may include dollar signs (` $ ` ) or
201
201
underscores (` _ ` ) but no other punctuation or special characters.
202
202
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] }}
204
204
205
205
Words with a special meaning, such as ` let ` , are _ ((keyword))s_ , and
206
206
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
216
216
switch this throw true try typeof var void while with yield
217
217
```
218
218
219
+ {{index [ syntax, error] }}
220
+
219
221
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
221
223
reserved word.
222
224
223
225
## The environment
224
226
225
- {{index "standard environment"}}
227
+ {{index "standard environment", [ browser, environment ] }}
226
228
227
229
The collection of bindings and their values that exist at a given time
228
230
is called the _ ((environment))_ . When a program starts up, this
229
231
environment is not empty. It always contains bindings that are part of
230
232
the language ((standard)), and most of the time, it also has bindings
231
233
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
233
235
currently loaded website and to read ((mouse)) and ((keyboard)) input.
234
236
235
237
## Functions
236
238
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 ] }}
241
243
242
244
A lot of the values provided in the default environment have the type
243
245
_ ((function))_ . A function is a piece of program wrapped in a value.
244
246
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
246
248
function that shows a little ((dialog box)) asking for user input. It
247
249
is used like this:
248
250
@@ -252,10 +254,10 @@ prompt("Enter passcode");
252
254
253
255
{{figure {url: "img/prompt.png", alt: "A prompt dialog", width: "8cm"}}}
254
256
255
- {{index parameter, [ function, application] }}
257
+ {{index parameter, [ function, application] , [ parentheses, arguments ] }}
256
258
257
259
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
259
261
expression that produces a function value. Usually you'll directly use
260
262
the name of the binding that holds the function. The values between
261
263
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.
270
272
271
273
## The console.log function
272
274
273
- {{index "JavaScript console", "developer tools", "Node.js", "console.log", output}}
275
+ {{index "JavaScript console", "developer tools", "Node.js", "console.log", output, [ browser, environment ] }}
274
276
275
277
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
277
279
` console.log ` function that writes out its arguments to _ some_ text
278
280
output device. In browsers, the output lands in the ((JavaScript
279
281
console)). This part of the browser interface is hidden by default,
@@ -295,12 +297,12 @@ console.log("the value of x is", x);
295
297
296
298
if}}
297
299
298
- {{index object}}
300
+ {{index [ object, property ] , [ property, access ] }}
299
301
300
302
Though binding names cannot contain ((period character))s,
301
303
` console.log ` does have one. This is because ` console.log ` isn't a
302
304
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
304
306
find out exactly what this means in [ Chapter ?] ( data#properties ) .
305
307
306
308
{{id return_values}}
@@ -366,7 +368,7 @@ control flow:
366
368
367
369
## Conditional execution
368
370
369
- {{index Boolean, "control flow"}}
371
+ {{index Boolean, [ "control flow", conditional ] }}
370
372
371
373
Not all programs are straight roads. We may, for example, want to
372
374
create a branching road, where the program takes the proper branch
@@ -375,7 +377,7 @@ execution))_.
375
377
376
378
{{figure {url: "img/controlflow-if.svg", alt: "Conditional control flow",width: "4cm"}}}
377
379
378
- {{index syntax, "Number function", "if keyword"}}
380
+ {{index [ syntax, statement ] , "Number function", "if keyword"}}
379
381
380
382
Conditional execution is created with the ` if ` keyword in JavaScript.
381
383
In the simple case, we want some code to be executed if, and only if,
@@ -392,9 +394,11 @@ if (!Number.isNaN(theNumber)) {
392
394
393
395
With this modification, if you enter "parrot", no output is shown.
394
396
397
+ {{index [ parentheses, statement] }}
398
+
395
399
The ` if ` keyword executes or skips a statement depending on the value
396
400
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
398
402
execute.
399
403
400
404
{{index "Number.isNaN function"}}
@@ -405,9 +409,9 @@ function happens to return `NaN` when you give it a string that
405
409
doesn't represent a valid number. Thus, the condition translates to
406
410
"unless ` theNumber ` is not-a-number, do this".
407
411
408
- {{index grouping, "{} (block)"}}
412
+ {{index grouping, "{} (block)", [ braces, "block" ] }}
409
413
410
- The statement after the ` if ` is wrapped in (( braces)) (` { ` and
414
+ The statement after the ` if ` is wrapped in braces (` { ` and
411
415
` } ` ) in this example. The braces can be used to group any number of
412
416
statements into a single statement, called a _ ((block))_ . You could
413
417
also have omitted them in this case, since they hold only a single
@@ -481,7 +485,7 @@ console.log(10);
481
485
console.log(12);
482
486
```
483
487
484
- {{index "control flow"}}
488
+ {{index [ "control flow", loop ] }}
485
489
486
490
That works, but the idea of writing a program is to make something
487
491
_ less_ work, not more. If we needed all even numbers less than 1,000,
@@ -491,7 +495,7 @@ _((loop))_.
491
495
492
496
{{figure {url: "img/controlflow-loop.svg", alt: "Loop control flow",width: "4cm"}}}
493
497
494
- {{index syntax, "counter variable"}}
498
+ {{index [ syntax, statement ] , "counter variable"}}
495
499
496
500
Looping control flow allows us to go back to some point in the program
497
501
where we were before and repeat it with our current program state. If
@@ -509,15 +513,15 @@ while (number <= 12) {
509
513
// … etcetera
510
514
```
511
515
512
- {{index "while loop", Boolean}}
516
+ {{index "while loop", Boolean, [ parentheses, statement ] }}
513
517
514
518
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
516
520
then a statement, much like ` if ` . The loop keeps entering that
517
521
statement as long as the expression produces a value that gives ` true `
518
522
when converted to Boolean.
519
523
520
- {{index comparison, state}}
524
+ {{index [ state, in binding ] , [ binding, as state] }}
521
525
522
526
The ` number ` binding demonstrates the way a ((binding)) can track the
523
527
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
550
554
?] ( data#array_indexing ) , it is a good idea to get used to
551
555
counting from 0.
552
556
553
- {{index "loop body", "do loop", "control flow"}}
557
+ {{index "loop body", "do loop", [ "control flow", loop ] }}
554
558
555
559
A ` do ` loop is a control structure similar to a ` while ` loop. It
556
560
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.
576
580
577
581
## Indenting Code
578
582
579
- {{index " code structure", whitespace, "programming style"}}
583
+ {{index [ code, " structure of" ] , [ whitespace, indentation ] , "programming style"}}
580
584
581
585
In the examples, I've been adding spaces in front of statements that
582
586
are part of some larger statement. These spaces are not required—the computer
@@ -608,7 +612,7 @@ amount.
608
612
609
613
## for loops
610
614
611
- {{index syntax, "while loop", "counter variable"}}
615
+ {{index [ syntax, statement ] , "while loop", "counter variable"}}
612
616
613
617
Many loops follow the pattern shown in the ` while ` examples. First a
614
618
"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) {
631
635
// … etcetera
632
636
```
633
637
634
- {{index "control flow", state}}
638
+ {{index [ "control flow", loop ] , state}}
635
639
636
640
This program is exactly equivalent to the
637
641
[ earlier] ( program_structure#loops ) even-number-printing example. The
638
642
only change is that all the ((statement))s that are related to the
639
643
"state" of the loop are grouped together after ` for ` .
640
644
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
642
648
((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
644
650
((expression)) that _ checks_ whether the loop must continue. The final
645
651
part _ updates_ the state of the loop after every iteration. In most
646
652
cases, this is shorter and clearer than a ` while ` construct.
@@ -714,7 +720,7 @@ iteration.
714
720
715
721
## Updating bindings succinctly
716
722
717
- {{index assignment, "+= operator", "-= operator", "/= operator", "* = operator", state, "side effect"}}
723
+ {{index assignment, "+= operator", "-= operator", "/= operator", "* = operator", [ state, in binding ] , "side effect"}}
718
724
719
725
Especially when looping, a program often needs to "update" a binding
720
726
to hold a value based on that binding's previous value.
@@ -747,7 +753,7 @@ equivalents: `counter++` and `counter--`.
747
753
748
754
## Dispatching on a value with switch
749
755
750
- {{index syntax, "conditional execution", dispatching , [ "if keyword", chaining] }}
756
+ {{index [ syntax, statement ] , "conditional execution", dispatch , [ "if keyword", chaining] }}
751
757
752
758
It is not uncommon for code to look like this:
753
759
@@ -782,7 +788,7 @@ switch (prompt("What is the weather like?")) {
782
788
}
783
789
```
784
790
785
- {{index fallthrough, comparison, "break keyword", "case keyword", "default keyword"}}
791
+ {{index fallthrough, "break keyword", "case keyword", "default keyword"}}
786
792
787
793
You may put any number of ` case ` labels inside the block opened by
788
794
` switch ` . The program will start executing at the label that
@@ -797,7 +803,7 @@ executed.
797
803
798
804
## Capitalization
799
805
800
- {{index capitalization, [ binding, naming] , whitespace}}
806
+ {{index capitalization, [ binding, naming] , [ whitespace, syntax ] }}
801
807
802
808
Binding names may not contain spaces, yet it is often helpful to use
803
809
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:
1036
1042
```
1037
1043
1038
1044
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
1040
1046
any ` size ` , outputting a grid of the given width and height.
1041
1047
1042
1048
{{if interactive
@@ -1052,7 +1058,7 @@ if}}
1052
1058
You can build the string by starting with an empty one (` "" ` ) and
1053
1059
repeatedly adding characters. A newline character is written ` "\n" ` .
1054
1060
1055
- {{index [ nesting, "of loops"] , braces}}
1061
+ {{index [ nesting, "of loops"] , [ braces, "block" ] }}
1056
1062
1057
1063
To work with two ((dimensions)), you will need a ((loop)) inside of a
1058
1064
loop. Put braces around the bodies of both loops to make it
0 commit comments