55
66= Values, Types, and Operators =
77
8+ [chapterquote="true"]
89[quote, Master Yuan-Ma, The Book of Programming]
910____
10- (((Yuan-Ma)))(((Book of Programming, The))) Below the surface of the
11+ Below the surface of the
1112machine, the program moves. Without effort, it expands and contracts.
1213In great harmony, electrons scatter and regroup. The forms on the
1314monitor are but ripples on the water. The essence stays invisibly
1415below.
1516____
1617
17- (((binary data)))(((data)))(((bit)))(((memory)))Inside the computer's
18- world, there is only data. You can read data, modify data, create new
18+ (((Yuan-Ma)))(((Book of Programming)))(((binary
19+ data)))(((data)))(((bit)))(((memory)))Inside the computer's world,
20+ there is only data. You can read data, modify data, create new
1921data—but anything that isn't data simply does not exist. All this data
2022is stored as long sequences of bits and is thus fundamentally alike.
2123
@@ -42,7 +44,7 @@ So that's the binary number 00001101, or 8 + 4 + 1, which equals 13.
4244
4345== Values ==
4446
45- (((memory)))(((volatile data storage)))(((hard disk )))Imagine a sea of
47+ (((memory)))(((volatile data storage)))(((hard drive )))Imagine a sea of
4648bits. An ocean of them. A typical modern computer has more than 30
4749billion bits in its volatile data storage. Nonvolatile storage (the
4850hard disk or equivalent) tends to have yet a few orders of magnitude
@@ -105,7 +107,7 @@ computers have plenty of memory, so you are free to use 64-bit chunks,
105107which means you need to worry about overflow only when dealing with
106108truly astronomical numbers.
107109
108- (((floating-point number)))(((fractional numbers )))(((sign bit)))Not
110+ (((sign)))((( floating-point number)))(((fractional number )))(((sign bit)))Not
109111all whole numbers below 18 quintillion fit in a JavaScript number,
110112though. Those bits also store negative numbers, so one bit indicates
111113the sign of the number. A bigger issue is that nonwhole numbers must
@@ -158,11 +160,11 @@ number from them. Here is what they look like in JavaScript:
158160100 + 4 * 11
159161----
160162
161- (((asterisk )))(((plus character )))(((pass:[*] operator)))(((+
162- operator)))The `+` and `*` symbols are called _operators_. The first
163- stands for addition, and the second stands for multiplication. Putting
164- an operator between two values will (((application,of
165- operators))) apply it to those values and produce a new value.
163+ (((operator,application )))(((asterisk )))(((plus
164+ character)))(((pass:[*] operator)))(((+ operator))) The `+` and `*`
165+ symbols are called _operators_. The first stands for addition, and the
166+ second stands for multiplication. Putting an operator between two
167+ values will apply it to those values and produce a new value.
166168
167169(((grouping)))(((parentheses)))(((precedence)))Does the example mean
168170“add 4 and 100, and multiply the result by 11”, or is the
@@ -220,8 +222,7 @@ numeric operations that don't yield a precise, meaningful result.
220222
221223== Strings ==
222224
223- (((syntax)))(((textual
224- data)))(((character)))(((string,notation)))(((single-quote
225+ (((syntax)))(((text)))(((character)))(((string,notation)))(((single-quote
225226character)))(((double-quote character)))(((quotation mark)))The next
226227basic data type is the _((string))_. Strings are used to represent
227228text. They are written by enclosing their content in quotes.
@@ -242,9 +243,9 @@ quotes between quotes might be hard. _Newlines_ (the characters you
242243get when you press Enter) also can't be put between quotes. The string
243244has to stay on a single line.
244245
245- (((escaping,in strings)))(((backslash)))To be able to have such
246- characters in a string, the following notation is used: whenever a
247- backslash (“\”) is found inside quoted text, it indicates that the
246+ (((escaping,in strings)))(((backslash character )))To be able to have
247+ such characters in a string, the following notation is used: whenever
248+ a backslash (“\”) is found inside quoted text, it indicates that the
248249character after it has a special meaning. This is called _escaping_
249250the character. A quote that is preceded by a backslash will not end
250251the string but be part of it. When an “n” character occurs after a
@@ -442,11 +443,11 @@ necessary:
4424431 + 1 == 2 && 10 * 10 > 50
443444----
444445
445- (((ternary operator )))(((?: operator)))(((conditional
446- operator)))(((colon character )))(((question mark )))The last logical
447- operator I will discuss is not unary, not binary, but _ternary_,
448- operating on three values. It is written with a question mark and a
449- colon, like this:
446+ (((conditional execution )))(((ternary operator)))(((?:
447+ operator)))(((conditional operator )))(((colon character )))(((question
448+ mark)))The last logical operator I will discuss is not unary, not
449+ binary, but _ternary_, operating on three values. It is written with a
450+ question mark and a colon, like this:
450451
451452[source,javascript]
452453----
@@ -480,7 +481,7 @@ recommend treating them as interchangeable (more on that in a moment).
480481
481482== Automatic type conversion ==
482483
483- (((NaN)))(((type conversion )))In the introduction, I mentioned that
484+ (((NaN)))(((type coercion )))In the introduction, I mentioned that
484485JavaScript goes out of its way to accept almost any program you give
485486it, even programs that do odd things. This is nicely demonstrated by
486487the following expressions:
@@ -509,11 +510,12 @@ to number). Yet in the third expression, `+` tries string
509510concatenation before numeric addition, so the `1` is converted to
510511`"1"` (from number to string).
511512
512- When something that doesn't map to a number in an obvious way (such as
513- `"five"` or `undefined`) is converted to a number, the value `NaN` is
514- produced. Further arithmetic operations on `NaN` keep producing
515- `NaN`, so if you find yourself getting one of those in an unexpected
516- place, look for accidental type conversions.
513+ (((type coercion)))(((number,conversion to)))When something that
514+ doesn't map to a number in an obvious way (such as `"five"` or
515+ `undefined`) is converted to a number, the value `NaN` is produced.
516+ Further arithmetic operations on `NaN` keep producing `NaN`, so if you
517+ find yourself getting one of those in an unexpected place, look for
518+ accidental type conversions.
517519
518520(((null)))(((undefined)))(((comparison,of undefined values)))(((==
519521operator)))When comparing values of the same type using `==`, the
@@ -537,7 +539,7 @@ That last piece of behavior is often useful. When you want to test
537539whether a value has a real value instead of `null` or `undefined`, you
538540can simply compare it to `null` with the `==` (or `!=`) operator.
539541
540- (((Boolean,conversion to)))(((=== operator)))(((!==
542+ (((type coercion)))((( Boolean,conversion to)))(((=== operator)))(((!==
541543operator)))(((comparison)))But what if you want to test whether
542544something refers to the precise value `false`? The rules for
543545converting strings and numbers to Boolean values state that `0`,
@@ -556,11 +558,12 @@ using the shorter operators.
556558
557559=== Short-circuiting of logical operators ===
558560
559- (((operator)))The logical operators `&&` and `||` handle values of
560- different types in a peculiar way. They will convert the value on
561- their left side to Boolean type in order to decide what to do, but
562- depending on the operator and the result of that conversion, they
563- return either the _original_ left-hand value or the right-hand value.
561+ (((type coercion)))(((Boolean,conversion to)))(((operator)))The
562+ logical operators `&&` and `||` handle values of different types in a
563+ peculiar way. They will convert the value on their left side to
564+ Boolean type in order to decide what to do, but depending on the
565+ operator and the result of that conversion, they return either the
566+ _original_ left-hand value or the right-hand value.
564567
565568(((|| operator)))The `||` operator, for example, will return the value
566569to its left when that can be converted to true and will return the
0 commit comments