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 ...
Get Learning JavaScript, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.