• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

2. Numbers and Dates > 2.3. Testing Numeric Equality

Testing Numeric Equality

NN 2, IE 3

Problem

You want to know whether two numeric values are equal (or not equal) before continuing processing.

Solution

Use the standard equality operator (= =) in a conditional statement:

if (firstNum =  = secondNum) {
    // OK, the number values are equal
}

Values on either side of the equality operator may be variables or numeric literals. Typical practice places the suspect value to the left of the operator, and the fixed comparison on the right.

Discussion

JavaScript has two types of equality operators. The fully backward-compatible, standard equality operator (= =) employs automatic data type conversion in some cases when the operands on either side are not of the same data type. Consider the following variable assignments:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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