The JavaScript Diaries: Part 5

By Lee Underwood


Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:
  <input type="button" value="Click for Greeting" onClick="greetVisitor();"

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.

Page 2 of 8

Previous Page
1 2 3 4 5 6 7 8
Next Page

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.

    By submitting your information, you agree that htmlgoodies.com may send you HTMLGOODIES offers via email, phone and text message, as well as email offers about other products and services that HTMLGOODIES believes may be of interest to you. HTMLGOODIES will process your information in accordance with the Quinstreet Privacy Policy.

Thanks for your registration, follow us on our social networks to keep up-to-date