Aliases for comparison operators



Following table shows the aliases for few of the Comparison operators. Suppose A holds 20 and variable B holds 20.

Operator Alias Example
= = (Equal) is A is B gives you true.
!= = (Not Equal) isnt A isnt B gives you false.

Example

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

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

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

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

c:/> coffee -c comparison_aliases.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 is b) is ");

  result = a === b;

  console.log(result);

  console.log("The result of (a isnt 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_aliases.coffee

On executing, the CoffeeScript file produces the following output.

The result of (a is b) is
false
The result of (a isnt b) is
true
coffeescript_operators_and_aliases.htm
Advertisements