please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 5 Next

 


5. Statements

Contents:
Compound Statements
if
while
for
for...in
break
continue
with
var
function
return
The Empty Statement
Summary of JavaScript Statements

As we saw in the last chapter, expressions are JavaScript "phrases" that can be evaluated to yield a value. Operators within an expression may have "side effects," but in general, expressions don't "do" anything. To make something happen, you use a JavaScript statement, which is akin to a complete sentence or command.

A JavaScript program is simply a collection of statements. Statements usually end with a semicolon. In fact, if you place each statement on a line by itself, you may omit the semicolon. There are circumstances in which you are required to use the semicolon, however, so it is a good idea to get in the habit of using it everywhere.

The following sections describe the various statements in JavaScript and explain their syntax.

5.1 Expression Statements

The simplest kind of statements in JavaScript are expressions that have side effects. We've seen this sort of statement in the section on operators in Chapter 4, Expressions and Operators. One major category of these are assignment statements. For example:

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

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

counter++;

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

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

These functions calls are expressions, but also produce an effect on the web browser, and so they are also statements. If a function does not have any side effects, then there is no sense in calling it, unless it is part of an assignment statement. So, 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);

Previous Home Next
Miscellaneous Operators Book Index Compound Statements


HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch05_01.htm, 6KB, , updated: 2019/10/14 15:00, local time: 2024/11/27 23:02,
TOP NEW HELP FIND: 
18.116.86.160:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/language/java/script/definitive/ch05_01.htm"> [Chapter 5] Statements</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .