Understanding Statements


JavaScript statements are the fundamental sentence-like building blocks used to create a program.

You probably have a fairly good, intuitive idea of what’s meant by statement, and you’ve already seen quite a few JavaScript statements in this chapter so far. Here are two examples of statements from earlier in this chapter:

 document.write(potterSays);  var trekQuote = trekVerb + trekObject; 

JavaScript statements consist of tokens, namely keywords, variables, literals, operators, function calls, object references, and so on. (Don’t be scared by all these terms; by the time you’ve finished Learn How to Program, you’ll be familiar with them all and eating them for breakfast!)

Do It Right!

All JavaScript statements you’ll see in this book are ended, also called terminated, with a semicolon. But JavaScript is a forgiving language. It’s not a good practice, but most of the time if you leave the semicolon off, JavaScript will still know what you mean and act accordingly.

Every JavaScript program consists of a sequence of statements that’s nominally processed from top to bottom. (The exception to this processing order is if you put flow control statements in your code that change the order of execution, as explained in Chapters 3 – 5.)

JavaScript ignores spaces, tabs, and most line breaks that occur between tokens, except those that are part of string literals. This means you can—and should—use spacing and line breaks to format code to be consistent and readable.

As I mentioned a moment ago, it’s normal practice to end each JavaScript statement with a semicolon. It’s a good idea to follow this practice because you’re less likely to inadvertently introduce errors. However, the semicolon isn’t required because JavaScript is the kind of language that always tries to fix laziness on the part of the programmer by guessing what the programmer means.

Advanced

Here’s an example of a situation in which JavaScript’s inclination to save you from yourself leads to trouble. If you break a line of code so that the line before the break appears to be a complete statement, JavaScript will assume that you omitted the semicolon accidentally and will insert one for you. So, the following:

 return  false; 

is evaluated as this:

 return;  false; 

which has a different meaning from the statement you probably intended:

 return false; 

Using Comments

Comments are a special and important kind of statement. Comments are intended to explain your code to human readers and aren’t processed as part of a computer program.

Do It Right!

It’s sometimes said that the best comments consist of clearly written code with proper naming of variables and other identifiers. This may be true, but it’s still an extremely good practice to add comments to your code.

Comments can also be used to identify who wrote a program. Because I’m proud of my code, I always sign it in a comment.

There are two styles of JavaScript comments. The first is to use // characters. Any text after the // and before the end of the line is treated as a comment and ignored by the JavaScript processor.

In the second kind of comment, any text between /* and */ is ignored by the processor.

Listing 2-4 shows five comments. To make sure you understand how comments are marked, see if you can count all five comments.

Listing 2.4: Styles of JavaScript Comments

start example
 // I'm a single-line comment  // This is another comment  /* Real multiple-line comment to follow */ //Yet another comment  /**************************************************************   * JavaScript module written by Harold Davis   * Purpose: Rock, Scissors, and Paper game   * Date:   * Inputs:   * Outputs:   * Of special note: Demonstrates conditional statements   **************************************************************/ 
end example




Learn How to Program Using Any Web Browser
Learn How to Program Using Any Web Browser
ISBN: 1590591135
EAN: 2147483647
Year: 2006
Pages: 115
Authors: Harold Davis

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net