What Is a Statement?
All JavaScript scripts are essentially a series of commands that are passed to the interpreter to be carried out sequentially. JavaScript usually requires that each one be placed on a separate line. So far, we have been referring to these lines simply as lines of code. It is more accurate to call them statements.
Already, you have come across several statements. For example, the method for adding comments into your code, which you learned in Chapter 1, uses the comment statements /* and */ or //. The // characters instruct the JavaScript interpreter to ignore all characters until the end of the line of code. Other examples include the following keywords: var, const, and function, which tell the interpreter to create a variable, constant, and function respectively. These are all statements.
It is important to realize that although the words "expression" and "statement" can occasionally be used interchangeably, there is a difference between the two. As you have seen, expressions use operators to manipulate data before they evaluate to a single value (with the possible exception of the assignment expressions that also assigns its value). This is great and an essential part of a programming or scripting language, but expressions can't do anything productive by themselves. Statements are the commands that decide what is done with the data that expressions return and, as you will see shortly, what is done with other statements. Expressions, on the other hand, are usually only a part of a statement, as shown in the example:
const MY_CONST = 72/9;
The 72/9 is a sub-expression of the assignment expression, which assigns the outcome of dividing 72 by 9 (the value 8) to the constant MY_CONST. However, the line, as a whole, is considered to be a const statement. It is the const keyword that causes the constant to be created, thereby doing something useful with the data that the expression to its right evaluates to.
Usually statements end with a line break, but if two statements are placed on the same line a semicolon must be used to separate each one. The semicolon tells the JavaScript interpreter that it has reached the end of a statement and should finish processing the code in front of the semicolon before it proceeds. In any case, it is helpful for the readability of your code to put no more than one statement on a line.
Let's study the type of statement that controls other statements.