Skip to content

Comparison and Logical Operators in Javascript

by David Yates

Logical and comparison operators are a means of comparing data types.

So given x = 5:

| Operator      | Description                   | How to use                                    | Result                                   |
| ------------- | ----------------------------- | --------------------------------------------- | ---------------------------------------- |
| ==            | equal to                      | x == 8 <br /> x==5 <br /> x == "5"            | `false` <br /> `true` <br /> `true`      |
| ===           | equal value and equal type    | x === 8 <br /> x === "5"                      | `true` <br /> `false`                    |
| !=            | not equal                     | x != 8                                        | `true`                                   |
| !==           | not equal to value or type    | x !== 8 <br /> x !== 5 <br /> x !== "5"       | `false` <br /> `true` <br /> `true`      |
| >             | greater than                  | x > 8                                         | `false`                                  |
| <             | less than                     | x < 8                                         | `true`                                   |
| >=            | greater than or equal to      | x == 8 <br /> x==5 <br /> x == "5"            | `false`                                  |
| <=            | less than or equal to         | x  == 8 <br /> x==5 <br /> x == "5"           | `true                                    |

The one thing worth noting here is the difference in comparison between double equals == and triple equals ===.

Double equals is fine for comparing values, but as an extra bit of safety it’s generally a good idea to use === where possible.

All Posts