
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses


Get two integers from the user.

Then,   display a message if the first integer is:

   less than, 
   equal to,   or 
   greater than
the second integer.



Test the condition   for each case separately,   so that   all three comparison operators are used   in the code.



Related task

   String comparison


 
// Using type coercion
function compare(a, b) {
  if (a==b) print(a + " equals " + b);
  if (a < b) print(a + " is less than " + b);
  if (a > b) print(a + " is greater than " + b);
}
 
// Without using type coercion and using standards
// Written for browsers
// assumption of a and b are both integers if typeof test passes
function compare (a, b) {
  if (typeof a === typeof b) {
    if (a === b) {
      document.writeln(a + " equals " + b);
    }
    if (a < b) {
      document.writeln(a + " is less than " + b);
    }
    if (a > b) {
      document.writeln(a + " is greater than " + b);
    }
  } else {
    // "1" and 1 are an example of this as the first is type string and the second is type number
    print(a + "{" + (typeof a) + "} and " + b + "{" + (typeof b) + "} are not of the same type and cannot be compared.");
  }
}
 