LEARNING JAVASCRIPT - Trang 103

CHAPTER 5

Expressions and Operators

An expression is a special kind of statement that evaluates to a value. The distinction

between an expression statement (which results in a value) and a non-expression

statement (which does not) is critical: understanding the difference gives you the

tools you need to combine language elements in useful ways.
You can think of a (nonexpression) statement as an instruction, and an expression

statement as a request for something. Imagine it’s your first day on the job, and the

foreman comes over and says, “Your job is to screw widget A into flange B.” That’s a

nonexpression statement: the foreman isn’t asking for the assembled part, merely

instructing you to do the assembly. If the foreman instead said, “Screw widget A into

flange B, and give it to me for inspection,” that would be equivalent to an expression

statement: not only are you being given an instruction, you’re also being asked to

return something. You may be thinking that either way something gets made: the

assembled part exists whether you set it back on the assembly line or give it to the

foreman for inspection. In a programming language, it’s similar: a nonexpression

statement usually does produce something, but only an expression statement results

in an explicit transfer of the thing that was produced.
Because expressions resolve to a value, we can combine them with other expressions,

which in turn can be combined with other expressions, and so on. Nonexpression

statements, on the other hand, might do something useful, but they cannot be com‐

bined in the same way.
Also because expressions resolve to a value, you can use them in assignments. That is,

you can assign the result of the expression to a variable, constant, or property. Let’s

consider a common operation expression: multiplication. It makes sense that multipli‐

cation is an expression: when you multiply two numbers, you get a result. Consider

two very simple statements:

79

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.