Statements, Blocks and Comments

It's all very well being able to store data, but most of the time it's useful to be able to do something with it. This means writing a JavaScript program or script fragment. A script fragment is just a piece of JavaScript that is so small it doesn't deserve to be called a whole program. Most of the examples in this chapter are script fragments. Regardless of what name it has, all JavaScript code consists of a sequence of statements. Statements are processed top-to-bottom, one at a time, unless you organize it otherwise. Everything is a statement unless it's a comment. Comments are human-friendly annotations which help the reader understand the script's function. Statements can be collected together into a block. A block has the advantage that it can act as if it were a single statement, even though it can contain more than one statement. Here's an example program that prints out different text depending upon the value of the variable 'result':

var result;
result = 15            // for this test, set it to 15
result = 5; result = 20;      // then set it again, just for fun

result 
= 19;               // final value is 19.

if ( result == 15 )
{
    /* this bit won't be printed
     because it's not 15
    */
    Clib.puts('The value is ')
    Clib.puts('still 15')
}
else
{
    /* this bit WILL be printed
       because it's not 15
    */
    Clib.puts('The value has been ');
    Clib.puts('changed to something else');
}
/* end of example */
;

Ignore the lines reading 'if..' and 'else ..' for the minute. Notice how some lines end with semi-colon (';') and some don't. In JavaScript, semi-colons are the official way of saying a statement has ended. However, JavaScript lets the script writer be care free—if a statement looks like it has finished even though there's no semi-colon, and then the line ends, JavaScript will assume you were lazy and act as though you did put one. It's good practice to ALWAYS put a semi-colon as it makes tracking down problems easier. All the examples to date show the simplest kind of statement: just assigning a value to a variable.

Note lines 3—6. Line 3 has two statements - 'result = 5;' and 'result = 20;'. Also, the single statement, 'result=19;' is split over lines 5 and 6. JavaScript is a free-format language. Provided you always use semi-colons, you can add as many spaces, tabs and blank lines as you like (whitespace), provided you stick to the spots where spaces can normally appear. This is similar to HTML. Just like HTML, using a spacing and indentation standard will make the code easier to understand in six months when it is revisited.

There are some comments in this example. The '//' characters tell the JavaScript interpreter to ignore everything until the end of the line—this is a single line comment. The '/*' characters tell the interpreter to ignore everything until the matching '*/'. This allows comments to span more than one line, although they can still be as small as part of a single line. You can't embed comments inside a String constant. Single line comments will nest, but multi-line ones won't. Additionally, single line comments can appear inside multi-line comments.

There are two blocks in the example above. Blocks are started by left-brace ('{') and ended by right-brace('}'). In this example each block contains two statements (and a comment, but that's ignored). The line reading 'if..' only operates on a single statement, so a block is used to make two statements behave like one. Notice that blocks break the rules: there is no need for a trailing semi-colon. This is the only exception.

A semi-colon by itself, as in the final line, is the 'do-nothing' statement, so a script reading just ';;;;;' would do nothing 5 times. Finally, since this script uses Clib.puts() it's clear this program will only run in a standalone JavaScript interpreter - if it was intended for a browser, document.write() would be used.

© 1997 by Wrox Press. All rights reserved.