Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
This chapter is from the book

JavaScript Statements

JavaScript programs are lists of individual instructions that we refer to as statements. To interpret statements correctly, the browser expects to find each statement written on a separate line:

this is statement 1
this is statement 2

Alternatively, they can be combined in the same line by terminating each with a semicolon:

this is statement 1; this is statement 2;

To ease the readability of your code, and to help prevent hard-to-find syntax errors, it’s good practice to combine both methods by giving each statement its own line and terminating the statement with a semicolon:

this is statement 1;
this is statement 2;

Commenting Your Code

Some statements are not intended to be executed by the browser’s JavaScript interpreter, but are there for the benefit of anybody who may be reading the code. We refer to such lines as comments, and there are specific rules for adding comments to your code.

A comment that occupies just a single line of code can be written by placing a double forward slash before the content of the line:

// This is a comment

To add a multiline comment in this way, we need to prefix every line of the comment:

// This is a comment
// spanning multiple lines

A more convenient way of entering multiline comments to your code is to prefix your comment with /* and terminate it with */. A comment written using this syntax can span multiple lines:

/*  This comment can span
    multiple lines
    without needing
    to mark up every line  */

Adding comments to your code is really a good thing to do, especially when you’re writing larger or more complex JavaScript applications. Comments can act as reminders to you, and also as instructions and explanations to anybody else reading your code at a later date.

  • + Share This
  • 🔖 Save To Your Account