DekGenius.com
Team LiB   Previous Section   Next Section

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 */ ;  
    Team LiB   Previous Section   Next Section