O'Reilly logo

Learning JavaScript, 3rd Edition by Ethan Brown

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required