please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 5
Statements
Next
 

5.3 if

The if statement is the fundamental "control statement" that allows JavaScript to "make decisions," or to execute statements conditionally. This statement has two forms. The first is:

if (expression) 
    statement

In this form, the expression is evaluated. If it is true, then statement is executed. If the expression is false, then statement is not executed. For example:

if (name ==index.html null)
    name = "John Doe";

Note that the parentheses around the expression are a required part of the syntax for the if statement. Although they look extraneous, they are actually a required part of the complete statement.

As mentioned above, we can always replace a single statement with a statement block. So the if statement might also look like this:

if ((address ==index.html null) || (address == "")) {
    address = "undefined";
    alert("Please specify a mailing address.");
}

Note that the indentation used in these examples is not mandatory. Extra spaces and tabs are ignored in JavaScript and since we used semicolons after all the primitive statements, these examples could be written all on one line if we wanted to. Using line breaks and indentation as shown here, however, makes the code easier to read and understand.

The second form of the if statement introduces an else clause that is executed when the expression is false. Its syntax is:

if (expression) 
    statement1
else
    statement2

In this form of the statement, the expression is evaluated, and if it is true, then statement1 is executed; otherwise statement2 is executed. For example:

if (name !=index.html null) 
    alert("Hello " + name + "\nWelcome to my home page.");
else {
    name = prompt("Welcome!\n What is your name?");
    alert("Hello " + name);
}

When you have nested if statements with else clauses, some caution is required to ensure that the else clause goes with the appropriate if statement. Consider the following lines:

i =index.html j = 1;
k = 2;
if (i == j) 
    if (j == k)
        document.write("i equals k");
else 
    document.write("i doesn't equal j");    // WRONG!!

In this example, the inner if statement forms the single statement allowed by the syntax of the outer if statement. Unfortunately, it is not clear (except from the hint given by the indentation) which if the else goes with. And in this example, the indenting "hint" is wrong, because a JavaScript interpreter will actually interpret the above as:

if (i ==index.html j) 
{
    if (j == k)
        document.write("i equals k");
    else 
        document.write("i doesn't equal j");    // OOPS!
}

The rule in JavaScript (as in most programming languages) is that an else clause is part of the nearest if statement. To make this example less ambiguous and easier to read, understand, maintain, and debug, you should use curly braces:

if (i ==index.html j) 
{
    if (j == k) {
        document.write("i equals k");
    }
}
else { // what a difference the location of a curly brace makes! 
    document.write("i doesn't equal j");
}


Previous Home Next
Compound Statements Book Index while

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

file: /Techref/language/JAVA/script/definitive/ch05_03.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/12/12 06:16,
TOP NEW HELP FIND: 
3.144.45.197: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://sxlist.com/Techref/language/JAVA/script/definitive/ch05_03.htm"> [Chapter 5] 5.3 if</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 sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .