logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA


dailyclick
All times in EST

Clairvoyance: 08:00 PM

Full Schedule
g
g JavaScript / Java Site

BellaOnline's JavaScript / Java Editor

g

Making Simple Decisions in JavaScript

Guest Author - Julie L Baumler

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!

Add Making+Simple+Decisions+in+JavaScript to Twitter Add Making+Simple+Decisions+in+JavaScript to Facebook Add Making+Simple+Decisions+in+JavaScript to MySpace Add Making+Simple+Decisions+in+JavaScript to Del.icio.us Digg Making+Simple+Decisions+in+JavaScript Add Making+Simple+Decisions+in+JavaScript to Yahoo My Web Add Making+Simple+Decisions+in+JavaScript to Google Bookmarks Add Making+Simple+Decisions+in+JavaScript to Stumbleupon Add Making+Simple+Decisions+in+JavaScript to Reddit




Getting Ready to Learn JavaScript
A First JavaScript Program
JavaScript/Java Newsletter
RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map


For FREE email updates, subscribe to the JavaScript / Java Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 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 BellaOnline Administration for details.

g


g features
Hearing Dog Tool - Adding Randomness

Hearing Dog Training Tool setTimeout()

GCJ - an Independant Java Implementation

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


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


BellaOnline Editor