# Introduction Comparison Operators

As well as Arithmetic functions can be used with integers, comparison operators can be used as well. These are required because most, if not all, programs will at some point need to compare values of variables in order to do something. To understand this, if it best to explain it in terms of a type of pseudo code which describes the actions of how the JavaScript code works in plain English :

if result of <variable>> due to comparison is certain value

• do this'

else

• do something else

A table of these comparison operators are as follows:

 Operator Description == Value 1 is equal to value 2 != Value 1 is not equal to value 2 > Value 1 is greater than value 2 >= Value 1 is greater than or equal to value 2 < Value 1 is less than value 2 <= Value 1 is less than or equal to value 2 === Value 1 is equal to value 2 and is same type !== Value 1 is not equal to value 2 and is same type

The last operators === and !== need to be explained a bit more. When comparing two values even if these are two different types they are the same, for example 1 and "1" are the same even though 1 is an integer and "1" is a string. If it didn't matter if a variable was a string and the other a variable, you would use == or != but if you needed to make sure that the two variables are numbers you would use === or !===.

It is possible to see the operators in action by running them in the console. The instructions to enter Seed console are here.

```print (3 ==3);
print (3 !=3);
print (3 == 5);
print (3 <= 5);
print (5 >= 5);
print (5 >= 3);
print (5 <= 3);```

When these commands are runs in the console a 1 or 0 is printed. 1 is true and 0 is false. What is happening is that the two values are compared and either true (1) or false (0). Each comparison returns true is comparison is correct.

The above examples are expalined as follows:

`print (3 ==3);`

3 is compared to see if it is equal to 3. As 3 is equal to 3, true (1) is returned.

`print (3 !=3);`

3 is compared to see if it is does not equal to 3. As 3 is equal to 3, it can't be not equal as the two values are the same so false (0) is returned. If the two values were different then true (1) would be returned.

`print (3 == 5);`

3 is compared to see if it is equal to 5. As 3 is not equal to 5, false (0) is returned.

`print (3 <= 5);`

3 is compared to see if it less than or equal to 5. As it is then true (1) is returned

`print (5 >= 5);`

5 is compared to see if it is more than or equal to 5. As it is then true(1) is returned.

`print (5 <= 3);`

5 is compared to see if it less than or equal to 3. As it is not less than or equal to 3, then false (0) is returned

It is advisable to amend the existing examples to use all the comparison operators in the above table until the logic of each operator is understood.

Projects/Seed/Tutorial/basic/comparison_intro (last edited 2013-11-22 19:19:53 by WilliamJonMcCann)