Section 6.1. Expression Statements


6.1. Expression Statements

The simplest kinds of statements in JavaScript are expressions that have side effects. This sort of statement was shown in Chapter 5. Assignment statements are one major category of expression statements. For example:

 s = "Hello " + name; i *= 3; 

The increment and decrement operators, ++ and --, are related to assignment statements. These have the side effect of changing a variable value, just as if an assignment had been performed:

 counter++; 

The delete operator has the important side effect of deleting an object property. Thus, it is almost always used as a statement, rather than as part of a larger expression:

 delete o.x; 

Function calls are another major category of expression statements. For example:

 alert("Welcome, " + name); window.close( ); 

These client-side function calls are expressions, but they also affect the web browser, so they are statements, too.

If a function does not have any side effects, there is no sense in calling it, unless it is part of an assignment statement. For example, you wouldn't just compute a cosine and discard the result:

 Math.cos(x); 

Instead, you'd compute the value and assign it to a variable for future use:

 cx = Math.cos(x); 

Again, please note that each line of code in each of these examples is terminated with a semicolon.




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