CoffeeScript - Aliases for logical operators



The following table shows the aliases for some of the logical operators. Suppose X holds true and variable Y holds false.

Operator Alias Example
&& (Logical AND) and X and Y gives you false
|| (Logical OR) or X or Y gives you true
! (not x) not not X gives you false

Example

The following example demonstrates the use aliases for logical operators in CoffeeScript. Save this code in a file with name logical_aliases.coffee.

a = true
b = false

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

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

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

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

c:\> coffee -c logical_aliases.coffee

On compiling, it gives you the following JavaScript.

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

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

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

  console.log("The result of not(a and 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 logical_aliases.coffee

On executing, the CoffeeScript file produces the following output.

The result of (a and b) is
false
The result of (a or b) is
true
The result of not(a and b) is
true
coffeescript_operators_and_aliases.htm
Advertisements