Table 5-10. Object and array operators
Operator
Description
Chapter
.
Member access
[]
Computed member access
in
Property existence operator
new
Object instantiation operator
instanceof
Prototype chain test operator
...
Spread operator
,
delete
Delete operator
Expressions in Template Strings
Template strings, which we introduced in
, can be used to inject the value of
any expression into a string. The example in
used a template string to dis‐
play the current temperature. What if we wanted to display the temperature differ‐
ence or display the temperature in degrees Fahrenheit instead of Celsius? We can use
expressions in template strings:
const
roomTempC
=
21.5
;
let
currentTempC
=
19.5
;
const
message
=
`The current temperature is `
+
`
${
currentTempC
-
roomTempC
}
\u00b0C different than room temperature.`
;
const
fahrenheit
=
`The current temperature is
${
currentTempC
*
9
/
5
+
32
}
\u00b0F`
;
Again we see the pleasing symmetry that expressions bring. We can use variables by
themselves in a template string because a variable is simply a type of expression.
Expressions and Control Flow Patterns
In
, we covered some common control flow patterns. Now that we’ve seen
some expressions that can affect control flow (ternary expressions and short-circuit
evaluation), we can cover some additional control flow patterns.
Converting if...else Statements to Conditional Expressions
Whenever an
if...else
statement is used to resolve a value—either as part of assign‐
ment, a smaller part of an expression, or the return value of a function—it is generally
100 | Chapter 5: Expressions and Operators