var x = 10

Now let’s use this variable for comparison to see how different statements are analysed (see Table 10.1).

Table 10.1    How different statements are analysed

Operator

Meaning

Description

Examples

==

Equal to

Determines if two values are the same

x == 10 // TRUE

x == 1 // FALSE

x == –1 // FALSE

x == “1” // TRUE

===

Equal value as well as type

This one checks to ensure that two values are both the same value as well as the same data type

x == 10 // TRUE

x == “10” // FALSE

!=

Is not equal to

Checks to see if values are NOT that same. If they are not the same, it returns TRUE

x != 1 // TRUE

x != 10 // FALSE

!==

Is not equal to or not the same type

Checks to see if values are NOT that same. If they are not the same, or are not the same type TRUE

x !== 1 // TRUE

x !== “10” // TRUE

x !== 10 // FALSE

>

Greater than

Checks to see if the value on the left side of the > is greater than the value on the right side. Returns TRUE if so

x > 1 // TRUE

x > 20 // FALSE

<

Less than

Checks to see if the value on the left side of the < is less than the value on the right side. Returns TRUE if so

x < 20 // TRUE

x < 10 // FALSE

>=

Greater than or equal to

Checks to see if the value on the left side of the >= is greater than or the same as the value on the right side. Returns TRUE if so

x >= 10 // TRUE

x >= 11 // FALSE

<=

Less than or equal to

Checks to see if the value on the left side of the <= is less than or the same as the value on the right side. Returns TRUE if so

x <= 10 // TRUE

x <= 9 // FALSE

Comparison operators are great for allowing us the ability to compare values in order to satisfy a condition. This opens up a world of possibilities for a programmer, who can create many ‘if this is the case, then do this’ operations in their code, and, as such create complex and powerful applications, which can respond to changes accordingly. At a basic level, this is what happens every time you log into any website. A check happens against your username and password; if they match the expected result, the statement returns TRUE and you are able to log in.

If the power of these comparison operators wasn’t already enough, we can also join them together to form complex queries that ask multiple questions. We can use the following logical operators to join conditional statements together to form complex queries like so: