Comparison operators

The other type of operators available in JavaScript are comparison operators . They are used to make assertions about the equality of two values (see Table 26-1).

Table 26-1. Comparison operators for equality

Operator

Meaning

>

Greater than

<

Less than

==

Equal to

!=

Not equal to

>=

Greater than or equal to

<=

Less than or equal to

There are also comparison operators that are used to assert identity (see Table 26-2).

Table 26-2. Comparison operators for identity

Operator

Meaning

===

Identical to

!==

Not identical to

To understand what identity is, take a look at two variables:

    var bool = true;
    var num  = 1;

bool is true, which is a Boolean value, and num is 1, a numeric value. If you recall back to the discussion of Booleans, however, you may recall that 1 and 0 are aliases for true and false, respectively. Therefore:

    bool == num;  /* bool is equal to num -or-
                     true and 1 are equal */

An identity-check, however, allows you to tell the two apart:

    bool !== num; /* bool is not identical to num -or-
                     true is not identical to 1 */

and that is why identity comparison operators are nice to have in your toolbox.

Get Web Design in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.