• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

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 true—and 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:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint