Whereas the string and number data types can have a virtually unlimited number of different values, the Boolean data type can only have two. They are the literals true and false. A Boolean value is a truth-value — it expresses the validity of a condition (tells whether the condition is true or not).
Comparisons you make in your scripts always have a Boolean outcome. Consider the following line of JScript code.
Here, the value of the variable x is tested to see if it is equal to the number 2000. If it is, the result of the comparison is the Boolean value true, which is assigned to the variable y. If x is not equal to 2000, then the result of the comparison is the Boolean value false.
Boolean values are especially useful in control structures. Here, you combine a comparison that creates a Boolean value directly with a statement that uses it. Consider the following JScript code sample.
if (x == 2000)
z = z + 1;
else
x = x + 1;
The if/else statement in JScript performs one action if a Boolean value is true (in this case, z = z + 1), and an alternate action if the Boolean value is false (x = x + 1).
You can use any expression as a comparative expression. Any expression that evaluates to 0, null, undefined, or an empty string is interpreted as false. An expression that evaluates to any other value is interpreted as true. For example, you could use an expression such as:
if (x = y + z) // This may not do what you expect — see below!
Note that the above line does not check if x is equal to y + z, since only a single equal sign (assignment) is used. Instead, the code above assigns the value of y + z to the variable x, and then checks if the result of the entire expression (the value of x) is zero. To check if x is equal to y + z, use the following code.
if (x == y + z) // This is different to the code above!
For more information on comparisons, see Controlling Program Flow.