Chapter 5. Grammar

The last major topic we want to tackle is how JavaScript’s language syntax works (aka its grammar). You may think you know how to write JS, but there’s an awful lot of nuance to various parts of the language grammar that lead to confusion and misconception, so we want to dive into those parts and clear some things up.

Note

The term “grammar” may be a little less familiar to readers than the term “syntax.” In many ways, they are similar terms, describing the rules for how the language works. There are nuanced differences, but they mostly don’t matter for our discussion here. The grammar for JavaScript is a structured way to describe how the syntax (operators, keywords, etc.) fits together into well-formed, valid programs. In other words, discussing syntax without grammar would leave out a lot of the important details. So our focus here in this chapter is most accurately described as grammar, even though the raw syntax of the language is what developers directly interact with.

Statements & Expressions

It’s fairly common for developers to assume that the term “statement” and “expression” are roughly equivalent. But here we need to distinguish between the two, because there are some very important differences in our JS programs.

To draw the distinction, let’s borrow from terminology you may be more familiar with: the English language.

A “sentence” is one complete formation of words that expresses a thought. It’s comprised of one or more “phrases,” each of which ...

Get You Don't Know JS: Types & Grammar 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.