JavaScript Chapter 4




Skip to Chapter   1  2  3  4  5  6  7  8  9  10 

                                                              Chapter 4 -- Conditions






The Definition of a Condition

Conditions compare two values to see if they are the same. On the condition that they are the same, a certain amount of code runs that normally wouldn't have.
A day-to-day life example of a condition would be a traffic light; When the traffic light is red you don't cross, when the traffic light is green you do... This scenario does not seem to apply in California, so I will give another one for all of you "Callies" out there.
Another condition would be if you get in a wreck, then your car insurance inevitably goes up.
Here is a small script I've written for the sole purpose of demonstrating how to use conditions:

please feel free to change the value of the description variable and see what the message changes to.

In Depth

We tell the computer that we want to compare the variable description with "fluffy!" on line 4.
We put this comparison inside of parenthesis, and after the magical word if(wich tells the computer that it should look for a comparison).
If the variable description is equal to the letters 'fluffy!'(which have to be in quotes so the computer doesn't confuse it as a variable), then it runs everything between '{' and '}'.
There are more ways than one to compare variables; I used the == (which means equal to) to show equivilancy, but you could use <(less than), >(greater than), and !=(not equal to).



The Else Command

There is another magical word in this script: else.
If description is not equal to "fluffy!" then it runs what is in the {}s after the else command.
Still with me?... I didn't think so.
If, however, you are still with me, I'd like to show you some of the many semi-useful purposes of the if condition.
Half-baked password logins(don't expect this to keep out anyone but the really lazy or ignorant).
To test whether someone wants to continue on.
Another valued use for conditions is to clear you of any legal stuff that would really hurt you later on.
In the fifth chapter you'll be using DHTML; this allows you to create ways to interact with the person at your web page
Variables DHTML DHTML