HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [141]
♦ Boolean function: Returns a true or false value, and you can also use this type of function as a condition.
Incidentally, Boolean variables are the only primitive variable type capitalized in most languages. They were named after a person, George Boole, a nineteenth-century mathematician who developed a form of binary arithmetic. Boole died thinking his research a failure. His work eventually became the foundation of modern computing. Drop a mention of George at your next computer science function to earn mucho geek points.
Comparison operators
JavaScript supports a number of comparison types, summarized in Table 2-1.
Table 2-1 Comparison Operators
Name
Operator
Example
Notes
Equality
==
(x==3)
Works with all variable types, including strings.
Not equal
!=
(x != 3)
True if values are not equal
Less than
<
(x < 3)
Numeric or alphabetical comparison
Greater than
>
(x > 3)
Numeric or alphabetical comparison
Less than or equal to
<=
(x <= 3)
Numeric or alphabetical comparison
Greater than or equal to
>=
(x >= 3)
Numeric or alphabetical comparison
You should consider a few things when working with conditions:
♦ Be sure the variable types are compatible. You’ll get unpredictable results if you compare a floating-point value to a string.
♦ You can compare string values. In JavaScript, you can use the inequality operators to determine the alphabetical order of two values. (This ability isn’t possible in most other languages.)
♦ Equality uses a double equal sign. The single equal sign ( = ) is used to indicate assignment. When you’re comparing variables, use a double equal ( == ) instead.
Don’t confuse assignment with comparison! If you accidentally say (x = 3) instead of (x == 3), your code won’t crash, but it won’t work properly. The first statement simply assigns the value 3 to the variable x. It returns the value true if the assignment was successful (which it will be). You’ll think you’re comparing x to 3, but you’re assigning 3 to x, and the condition will always be true. Keeping these two straight is a nightmare. I still mess it up once in a while.
Using the else Clause
The deuce.html game, described in the section “Using if to control flow,” is pretty exciting and all, but it would be even better if you had one comment when the roll is a 2 and another comment when it’s something else. Figure 2-3 shows a program with exactly this behavior.
Figure 2-3: You get one message for deuces and another message for everything else.
This program uses the same type of condition as the earlier deuce.html game, but it adds an important section:
The if statement is unchanged, but now an else clause appears. Here’s how the program works:
1. The if statement sets up a condition.
The if statement indicates the beginning of a code branch, and it prepares the way for a condition.
2. The condition establishes a test.
Conditions are true or false expressions, so the condition indicates something that can be true or false.
3. If the condition is true, the code between the condition and the else clause runs.
After this code is finished, control moves past the end of the if structure.
4. If the condition is false, the code between else and the end of the if statement runs instead.
The else clause acts like a fork in the road. The code goes along one path or another (depending on the condition) but never both paths at one time.
You can put as much code as you want inside an if or else clause, including more if statements!
The else clause is used only in the context of an if statement. You can’t use else by itself.
Using if-else for more complex interaction
The if-else structure is pretty useful when