Comparison Operators


Comparison operators compare their two operands and return a Boolean value of true or false . As you recall from our discussion of data types, Boolean values can take only two values: true or false. This is very useful when you're testing your data to see whether various conditions are true or false, as when you test whether more people voted for pizza for dinner than for hamburgers, and so on.

To make this more clear, consider this example. The comparison operators are usually used with JavaScript statements, such as the if statement (discussed later in this chapter), and I'll use them in if statements here. An if statement enables you to execute code or not depending on whether a condition is trueand you can specify the condition with comparison operators. In this example, I'm comparing the number of votes for pizza to the number of votes for hamburgers. If there are more pizza votes than hamburger votes , the expression votesForPizza > votesForHamburgers that uses the > (greater-than) comparison operator will return a value of true, which means the code enclosed in curly braces, { and }, in the if statement will be executed and the message Pizza wins! will appear in the web page:

 var votesForPizza = 5  var votesForHamburgers = 6  if(votesForPizza > votesForHamburgers) {  document.write("Pizza wins!")  } 

If the expression votesForPizza > votesForHamburgers is false, however, the code enclosed in curly braces in the if statement will not be executed. So I add additional if statements, using the < ( less-than ) and == (equal-to) comparison operators like this:

(Listing 02-08.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Working With Comparison Operators          </TITLE>      </HEAD>      <BODY>          <H1>Working With Comparison Operators</H1>          <SCRIPT LANGUAGE="JavaScript">          <!--              var votesForPizza = 5               var votesForHamburgers = 6  if(votesForPizza > votesForHamburgers) {   document.write("Pizza wins!")   }   if(votesForPizza < votesForHamburgers) {   document.write("Hamburgers win!")   }   if(votesForHamburgers == votesForPizza ) {   document.write("Tie!")   }  // -->          </SCRIPT>       </BODY>  </HTML> 

You can see the results in Figure 2.6, where hamburgers win. Getting to know the comparison operators is very important in JavaScript programming. Table 2.7 lists these operators.

Figure 2.6. Using the comparison operators.

graphics/02fig06.gif

Table 2.7. JavaScript Comparison Operators

Operator

Does This

==

Equality operator. Evaluates to true if both operands are equal. Be careful to use == when checking for equality, not = .

!=

Evaluates to true if the two operands are not equal.

===

Strict equality operator. Evaluates to true if the operands are equaland they also must be of the same type.

!==

Evaluates to true if the operands are either not equal or not of the same type.

>

Greater-than operator. Evaluates to true if the left operand is greater than the right operand either numerically , lexicographically, or using object data conversion functions.

>=

Greater-than-or-equal-to operator. Evaluates to true if the left operand is greater than or equal to the right operand either numerically, lexicographically, or using object data conversion functions.

<

Less-than operator. Evaluates to true if the left operand is less than the right operand either numerically, lexicographically, or using object data conversion functions.

<=

Less-than-or-equal-to operator. Evaluates to true if the left operand is less than or equal to the right operand either numerically, lexicographically, or using object data conversion functions.

Note, in particular, the equality comparison operator, == :

  if(votesForHamburgers == votesForPizza ) {   document.write("Tie!")   }  

The first thing to note is that to check whether two values are equal, you must use the == comparison operator , not the = assignment operator . I'm stressing that in particular, because using = when you should use == is a common error. If I had said votesForHamburgers = votesForPizza here, that would just have assigned the value in votesForPizza to votesForHamburgers , without making a comparison.

So when are values considered equal? Two strings are considered equal when they have the same sequence of characters , and the same length. Two numbers are considered equal when they are numerically equal. Two objects are equal if they refer to the same object. And two Booleans are equal if they are both either true or false.

It's easy to see how JavaScript can compare numbers, but what about comparing strings? When is one string "greater" than another? This kind of comparison is done lexicographically, which means, for example, that when you compare apples to oranges, "oranges" is considered greater than "apples" because "oranges" comes after "apples" alphabetically .

When comparing two values, JavaScript automatically tries to convert them to be of the same type for the comparison. When you're comparing a string and a number, JavaScript tries to convert the string to a numeric value. If you're comparing a Boolean value to a number, true is converted to 1 and false to 0. If you compare an object to a string or number, JavaScript checks whether the object has a method named toString for string comparisons, and valueOf for numeric comparisons. If not, it creates an error.

You might also notice the two other equality operators in Table 2.6, first introduced in JavaScript 1.3: === (strict equals) and !== (strict not equals). If you want to make sure two values are the same and that they are of the same data type, you use the strict equals and strict not equals operators instead.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net