A JavaScript statement is an instruction to tell the computer to perform a specific task. In the last section on variables, we introduced the assignment operator, which assigns a specific value to a variable. Assuming that we have defined a variable called 'total', a simple assignment statement might appear in our code that assigns the value 10 to that variable:
total = 10;
It's important to understand simple statements in order to understand compound statements. A compound statement is one statement that consists of several simple statements grouped together by brackets. Programming languages are often structured to expect a single instruction to perform a task. That task, however, may require many steps to complete, requiring the programmer to group them together in brackets, which might look like:
{ total = value1 + value2; document.writeln(total); }
This example would add value1 and value2, assign the result to the variable 'total' and then write that value to the screen.
In JavaScript, whitespaces (spaces, tabs and carriage-returns) do not affect the program. Therefore, the following code would be perfectly correct:
total =temp +2 ;
However, such code is confusing to read, and difficult to understand. The rule of thumb is to use whitespaces to make your code more readable. In later sections, the issue of program readability will be discussed where it applies to specific examples, but, generally, statements should be placed one per line, and compound statements enclosed in brackets should be indented by a tab to indicate their grouping. Above all, consistency is the most important consideration.