CoffeeScript - Comparison operators



JavaScript supports the following comparison operators. Assume variable A holds 10 and variable B holds 20, then −

Sr.No Operator and Description Example
1

= = (Equal)

Checks if the value of two operands are equal or not, if yes, then the condition becomes true.

(A == B) is not true.
2

!= (Not Equal)

Checks if the value of two operands are equal or not, if the values are not equal, then the condition becomes true.

(A != B) is true.
3

> (Greater than)

Checks if the value of the left operand is greater than the value of the right operand, if yes, then the condition becomes true.

(A > B) is not true.
4

< (Less than)

Checks if the value of the left operand is less than the value of the right operand, if yes, then the condition becomes true.

(A < B) is true.
5

>= (Greater than or Equal to)

Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes, then the condition becomes true.

(A >= B) is not true.
6

<= (Less than or Equal to)

Checks if the value of the left operand is less than or equal to the value of the right operand, if yes, then the condition becomes true.

(A <= B) is true.

Example

The following code shows how to use comparison operators in CoffeeScript. Save this code in a file with name comparison_example.coffee

a = 10
b = 20
console.log "The result of (a == b) is "
result = a == b
console.log result

console.log "The result of (a < b) is "
result = a < b
console.log result

console.log "The result of (a > b) is "
result = a > b
console.log result

console.log "The result of (a != b) is "
result = a != b
console.log result

console.log "The result of (a >= b) is "
result = a <= b
console.log result

console.log "The result of (a <= b) is "
result = a >= b
console.log result

Open the command prompt and compile the comparison_example.coffee file as shown below.

c:/> coffee -c comparison_example.coffee

On compiling, it gives you the following JavaScript.

// Generated by CoffeeScript 1.10.0
(function() {
  var a, b, result;
  a = 10;
  b = 20;

  console.log("The result of (a == b) is ");
  result = a === b;
  console.log(result);

  console.log("The result of (a < b) is ");
  result = a < b;
  console.log(result);

  console.log("The result of (a > b) is ");
  result = a > b;
  console.log(result);

  console.log("The result of (a != b) is ");
  result = a !== b;
  console.log(result);

  console.log("The result of (a >= b) is ");
  result = a <= b;
  console.log(result);

  console.log("The result of (a <= b) is ");
  result = a >= b;
  console.log(result);

}).call(this);

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:/> coffee comparison_example.coffee

On executing, the CoffeeScript file produces the following output.

The result of (a == b) is
false
The result of (a < b) is
true
The result of (a > b) is
false
The result of (a != b) is
true
The result of (a >= b) is
true
The result of (a <= b) is
false
coffeescript_operators_and_aliases.htm
Advertisements