Section 6.19. The Empty Statement


6.19. The Empty Statement

One final legal statement in JavaScript is the empty statement. It looks like this:

 ; 

Executing the empty statement obviously has no effect and performs no action. You might think there would be little reason to ever use such a statement, but the empty statement is occasionally useful when you want to create a loop that has an empty body. For example:

 // Initialize an array a for(i=0; i < a.length; a[i++] = 0) ; 

Note that the accidental inclusion of a semicolon after the right parenthesis of a for loop, while loop, or if statement can cause frustrating bugs that are difficult to detect. For example, the following code probably does not do what the author intended:

 if ((a == 0) || (b == 0));   // Oops! This line does nothing...     o = null;                // and this line is always executed. 

When you intentionally use the empty statement, it is a good idea to comment your code in a way that makes it clear that you are doing it on purpose. For example:

 for(i=0; i < a.length; a[i++] = 0) /* Empty */ ; 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

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