LEARNING JAVASCRIPT - Trang 124

Table 5-10. Object and array operators

Operator

Description

Chapter

.

Member access

Chapter 3

[]

Computed member access

Chapter 3

in

Property existence operator

Chapter 9

new

Object instantiation operator

Chapter 9

instanceof

Prototype chain test operator

Chapter 9

...

Spread operator

Chapter 6

,

Chapter 8

delete

Delete operator

Chapter 3

Expressions in Template Strings

Template strings, which we introduced in

Chapter 3

, can be used to inject the value of

any expression into a string. The example in

Chapter 3

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

Chapter 4

, 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

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.