The "if" or "if/else" structure can be used to provide flow control in your Java Script code. Flow control determines which statements are executed based upon the results of a test or condition. An "if/else" structure is coded as shown in this article.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

The if/else Structure

The if or if/else structure can be used to provide flow control in your Java Script code. Flow control determines which statements are executed based upon the results of a test or condition. An if/else structure is coded as shown below.

if(comparison)
{
   statements;
}
else
{
   statements;
}

The condition defined between the brackets frequently consists of a Comparison operator which compares the values of two variables, or compares the value of a variable to a constant value, as shown below.

if(x <= 100)
{
   document.write("x is less than or equal to 100");
}
else
{
   document.write("x is greater than 100");
}

The statement within the if brackets compares the value of a variable named x to 100. If x is less than or equal to 100, the statement within the first curly brackets is executed ("x is less than or equal to 100" is printed). If x is greater than or 100, the statement within the second curly brackets (following else is executed.

The if section is frequently used alone, without an accompanying else section, as shown below.

if(x <= 100)
{
   document.write("x is less than or equal to 100");
}

In this situation if the condition defined in the if statement is true, the statement within the curly brackets is executed. If the condition defined in the if statement is false, the program flow will continue with any code after the if structure.

Within the curly brackets there can be any number of statements. The example below shows two lines of code within each section, but it's possible to have thousands of lines of codes within each section.

if(x <= 100)
{
   document.write("x is less than or equal to 100");
   x = x + 50;
}
else
{
   document.write("x is greater than 100");
   x = x - 50;
}

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269