g
Printer Friendly Version

editor  
BellaOnline's JavaScript / Java Editor
 

Making Simple Decisions in JavaScript

The main reason to use JavaScript, particularly in web pages, is so that you can provide a different result depending on the situation. In order to do this, you need to be able to make decisions on which result to provide. JavaScript includes a number of ways to decide what to do based on what is going on at a particular moment (program state.) In programming terms, this process is also known as "control flow", because it controls which parts of the program run or which order they run in. The simplest and most common of these control flow statements is the if statement. The if statement is used as follows:

if (Test for something)
{
JavaScript statements for when the test is true
}
else
{
JavaScript statements for when the test is false
}

Your test goes between the parenthesis, the test must be a JavaScript expression that has a value of true or false. To make life easy for you, numbers (or the result of an equation) other than zero (0) and strings (characters in quotes) are true. The number zero and an empty string ("") are false. As you learn more JavaScript you will find that there are a large number of functions and expressions that return true or false that you can use in your tests.

Your JavaScript statements go between the curly braces ({ and }). If you have only one JavaScript statement, you don't actually need the curly braces, but my experience is that as soon as I leave out the curly braces, I need to add a second statement. I also find that always including the curly braces makes it really clear which code belongs to your if statement and which code belongs to the rest of the program when you look at it.

When this JavaScript runs, if your test result is true, the JavaScript between the first set of curly braces will be run and the JavaScript between the second set of curly braces will be ignored. If it is false, the JavaScript between the second set of curly braces is run and the first set's JavaScript is ignored. This allows you to decide what your program will do depending on the circumstances.

Sometimes, if the test is false, you don't want to do anything. In this case, you can leave out the else and the second set of curly braces. Sometimes you want to test for one of several things. Often the best way to do this is to combine a series of if ... else statements. When you do this, you usually write it as:

if (First Test)
{
JavaScript Statements for First Test
}
else if (Second Test)
{
JavaScript Statements for Second Test
}
else
{
JavaScript Statements for When All Tests are False
}

Now that you know how to make multiple decisions, how would you change the program in the A First JavaScript Program article so that it says "Good Afternoon" if it is run during the afternoon? Check here to see one way to do this.

This site needs an editor - click to learn more!

JavaScript / Java Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact Editor Wanted for details.



| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor