spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / diaries / 5

[previous] [next]

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

The JavaScript Diaries: Part 5

The if/else Statement

Another type of conditional statement is called an if/else statement. It adds another dimension to the if statement. This statement allows for more complex decisions to be made within the script. The format is:

  if (condition) {
    action to be taken if the condition is true
  }
  else {
    action to be taken if the condition is false
  }

The else reserved word allows a determined action to be taken if the first condition is false. In other words, one way or another this portion of the script will be executed. If the condition is true, then the first statement will be executed; if the condition is false, the second statement will execute. Execution will then return to the main portion of the script.

Building upon our previous script, let's change it to an if/else statement.

  var userName = prompt("Please enter your name.", "");
  if ( (userName == "") || (userName == null) ) {
    alert("Don't you even know your own name?");
  }
  else {
    alert("Welcome " + userName + "!");
  }

Ah, now we're starting to do something that makes sense; this is a simple validation routine. (This should look familiar. We used it in our previous section on functions.) Here are the steps involved:

  1. The variable userName is declared and given the results of a prompt window as its value.
  2. A condition statement checks to see if any data is entered. It doesn't matter whether it is a string or a number.
    1. Our condition statement says this: if nothing is entered in the prompt window, the condition statement is true and an alert window will display the result: "Don't you even know your own name?"
  3. If the condition statement is false (e.g., data was entered into the prompt window), then an alert window will be displayed with the result: "Welcome [entered data]."

Instead of having the script run each time the page is accessed, it could be put into a function and run from a link:

  function greetVisitor() {
    var myName = prompt("Please enter your name.", "");
    if ( (myName == "") || (myName == null) ) {
      alert("Don't you even know your own name?");
    }
    else {
      alert("Welcome " + myName + "!");
    }
  }
In the body of the document, insert the following code:
<form>
  <input type="button" value="Click for Greeting" onClick="greetVisitor();"
</form>

Is it starting to come together now?

The else if Statement

When you need to check multiple conditions, you can use the else if statement. It works like this (adapted with thanks to Andrew and Jonathan Watt):

  var promptVal = prompt("Enter a number:","");

  if (promptVal > 0) {
    alert("The number you entered was positive.");
  }
  else if (promptVal == 0) {
    alert("The number you entered was 0.");
  }
  else {
   alert("The number you entered was negative.");
  }

You can include as many else if statements as needed to obtain the desired results. Let's create a script with a conditional statement from scratch and see how it's done.

home / programming / javascript / diaries / 5

[previous] [next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: June 10, 2005

URL: